Skip to content

Commit

Permalink
test: update test to use the multiple items functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-Torrent committed Aug 17, 2021
1 parent 2767189 commit 38fef57
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 26 deletions.
16 changes: 8 additions & 8 deletions cypress/integration/item/copy/gridCopyItem.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ describe('Copy Item in Grid', () => {
const { id: toItem, path: toItemPath } = SAMPLE_ITEMS.items[1];
copyItem({ id: copyItemId, toItemPath });

cy.wait('@copyItem').then(({ response: { body } }) => {
cy.wait('@copyItems').then(({ response: { body } }) => {
cy.get(`#${buildItemCard(copyItemId)}`).should('exist');

// check in new parent
cy.goToItemInGrid(toItem);
cy.get(`#${buildItemCard(body.id)}`).should('exist');
cy.get(`#${buildItemCard(body[0].id)}`).should('exist');
});
});

Expand All @@ -49,12 +49,12 @@ describe('Copy Item in Grid', () => {
const { id: toItem, path: toItemPath } = SAMPLE_ITEMS.items[3];
copyItem({ id: copyItemId, toItemPath });

cy.wait('@copyItem').then(({ response: { body } }) => {
cy.wait('@copyItems').then(({ response: { body } }) => {
cy.get(`#${buildItemCard(copyItemId)}`).should('exist');

// check in new parent
cy.goToItemInGrid(toItem);
cy.get(`#${buildItemCard(body.id)}`).should('exist');
cy.get(`#${buildItemCard(body[0].id)}`).should('exist');
});
});

Expand All @@ -71,12 +71,12 @@ describe('Copy Item in Grid', () => {
const toItemPath = ROOT_ID;
copyItem({ id: copyItemId, toItemPath });

cy.wait('@copyItem').then(({ response: { body } }) => {
cy.wait('@copyItems').then(({ response: { body } }) => {
cy.get(`#${buildItemCard(copyItemId)}`).should('exist');

// check in new parent
cy.goToHome();
cy.get(`#${buildItemCard(body.id)}`).should('exist');
cy.get(`#${buildItemCard(body[0].id)}`).should('exist');
});
});

Expand All @@ -94,10 +94,10 @@ describe('Copy Item in Grid', () => {
const { path: toItemPath } = SAMPLE_ITEMS.items[0];
copyItem({ id: copyItemId, toItemPath });

cy.wait('@copyItem').then(({ response: { body } }) => {
cy.wait('@copyItems').then(({ response: { body } }) => {
// check item is still existing in parent
cy.get(`#${buildItemCard(copyItemId)}`).should('exist');
cy.get(`#${buildItemCard(body.id)}`).should('not.exist');
cy.get(`#${buildItemCard(body[0].id)}`).should('not.exist');
});
});
});
Expand Down
16 changes: 8 additions & 8 deletions cypress/integration/item/copy/listCopyItem.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ describe('Copy Item in List', () => {
const { id: toItem, path: toItemPath } = SAMPLE_ITEMS.items[1];
copyItem({ id: copyItemId, toItemPath });

cy.wait('@copyItem').then(({ response: { body } }) => {
cy.wait('@copyItems').then(({ response: { body } }) => {
cy.get(`#${buildItemsTableRowId(copyItemId)}`).should('exist');

// check in new parent
cy.goToItemInList(toItem);
cy.get(`#${buildItemsTableRowId(body.id)}`).should('exist');
cy.get(`#${buildItemsTableRowId(body[0].id)}`).should('exist');
});
});

Expand All @@ -61,12 +61,12 @@ describe('Copy Item in List', () => {
const { id: toItem, path: toItemPath } = SAMPLE_ITEMS.items[3];
copyItem({ id: copyItemId, toItemPath });

cy.wait('@copyItem').then(({ response: { body } }) => {
cy.wait('@copyItems').then(({ response: { body } }) => {
cy.get(`#${buildItemsTableRowId(copyItemId)}`).should('exist');

// check in new parent
cy.goToItemInList(toItem);
cy.get(`#${buildItemsTableRowId(body.id)}`).should('exist');
cy.get(`#${buildItemsTableRowId(body[0].id)}`).should('exist');
});
});

Expand All @@ -84,12 +84,12 @@ describe('Copy Item in List', () => {
const { id: copyItemId } = SAMPLE_ITEMS.items[2];
copyItem({ id: copyItemId, toItemPath: ROOT_ID });

cy.wait('@copyItem').then(({ response: { body } }) => {
cy.wait('@copyItems').then(({ response: { body } }) => {
cy.get(`#${buildItemsTableRowId(copyItemId)}`).should('exist');

// check in new parent
cy.goToHome();
cy.get(`#${buildItemsTableRowId(body.id)}`).should('exist');
cy.get(`#${buildItemsTableRowId(body[0].id)}`).should('exist');
});
});

Expand All @@ -109,10 +109,10 @@ describe('Copy Item in List', () => {
const { path: toItemPath } = SAMPLE_ITEMS.items[0];
copyItem({ id: copyItemId, toItemPath });

cy.wait('@copyItem').then(({ response: { body } }) => {
cy.wait('@copyItems').then(({ response: { body } }) => {
// check item is still existing in parent
cy.get(`#${buildItemsTableRowId(copyItemId)}`).should('exist');
cy.get(`#${buildItemsTableRowId(body.id)}`).should('not.exist');
cy.get(`#${buildItemsTableRowId(body[0].id)}`).should('not.exist');
});
});
});
Expand Down
10 changes: 5 additions & 5 deletions cypress/integration/item/move/gridMoveItem.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('Move Item in Grid', () => {
const { id: toItem, path: toItemPath } = SAMPLE_ITEMS.items[1];
moveItem({ id: movedItem, toItemPath });

cy.wait('@moveItem').then(({ request: { url, body } }) => {
cy.wait('@moveItems').then(({ request: { url, body } }) => {
expect(body.parentId).to.equal(toItem);
expect(url).to.contain(movedItem);
});
Expand All @@ -51,7 +51,7 @@ describe('Move Item in Grid', () => {
const { id: toItem, path: toItemPath } = SAMPLE_ITEMS.items[3];
moveItem({ id: movedItem, toItemPath });

cy.wait('@moveItem').then(({ request: { body, url } }) => {
cy.wait('@moveItems').then(({ request: { body, url } }) => {
expect(body.parentId).to.equal(toItem);
expect(url).to.contain(movedItem);
});
Expand All @@ -70,15 +70,15 @@ describe('Move Item in Grid', () => {
const toItem = ROOT_ID;
moveItem({ id: movedItem, toItemPath: toItem });

cy.wait('@moveItem').then(({ request: { body, url } }) => {
cy.wait('@moveItems').then(({ request: { body, url } }) => {
expect(body.parentId).to.equal(undefined);
expect(url).to.contain(movedItem);
});
});

describe('Error handling', () => {
it('error while moving item does not create in interface', () => {
cy.setUpApi({ ...SAMPLE_ITEMS, moveItemError: true });
cy.setUpApi({ ...SAMPLE_ITEMS, moveItemsError: true });
const { id } = SAMPLE_ITEMS.items[0];

// go to children item
Expand All @@ -90,7 +90,7 @@ describe('Move Item in Grid', () => {
const { path: toItemPath } = SAMPLE_ITEMS.items[3];
moveItem({ id: movedItem, toItemPath });

cy.wait('@moveItem').then(() => {
cy.wait('@moveItems').then(() => {
// check item is still there
cy.get(`#${buildItemCard(movedItem)}`).should('exist');
});
Expand Down
10 changes: 5 additions & 5 deletions cypress/integration/item/move/listMoveItem.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('Move Item in List', () => {
const { id: toItem, path: toItemPath } = SAMPLE_ITEMS.items[1];
moveItem({ id: movedItem, toItemPath });

cy.wait('@moveItem').then(({ request: { url, body } }) => {
cy.wait('@moveItems').then(({ request: { url, body } }) => {
expect(body.parentId).to.equal(toItem);
expect(url).to.contain(movedItem);
});
Expand All @@ -62,7 +62,7 @@ describe('Move Item in List', () => {
const { id: toItem, path: toItemPath } = SAMPLE_ITEMS.items[3];
moveItem({ id: movedItem, toItemPath });

cy.wait('@moveItem').then(({ request: { body, url } }) => {
cy.wait('@moveItems').then(({ request: { body, url } }) => {
expect(body.parentId).to.equal(toItem);
expect(url).to.contain(movedItem);
});
Expand All @@ -84,15 +84,15 @@ describe('Move Item in List', () => {
const toItem = ROOT_ID;
moveItem({ id: movedItem, toItemPath: toItem });

cy.wait('@moveItem').then(({ request: { body, url } }) => {
cy.wait('@moveItems').then(({ request: { body, url } }) => {
expect(body.parentId).to.equal(undefined);
expect(url).to.contain(movedItem);
});
});

describe('Error handling', () => {
it('error while moving item does not create in interface', () => {
cy.setUpApi({ ...SAMPLE_ITEMS, moveItemError: true });
cy.setUpApi({ ...SAMPLE_ITEMS, moveItemsError: true });
const { id } = SAMPLE_ITEMS.items[0];

// go to children item
Expand All @@ -107,7 +107,7 @@ describe('Move Item in List', () => {
const { path: toItemPath } = SAMPLE_ITEMS.items[3];
moveItem({ id: movedItem, toItemPath });

cy.wait('@moveItem').then(() => {
cy.wait('@moveItems').then(() => {
// check item is still there
cy.get(`#${buildItemsTableRowId(movedItem)}`).should('exist');
});
Expand Down
8 changes: 8 additions & 0 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import {
} from '../../src/config/selectors';
import {
mockCopyItem,
mockCopyItems,
mockDeleteItem,
mockGetChildren,
mockGetItem,
mockGetOwnItems,
mockMoveItem,
mockMoveItems,
mockPostItem,
mockEditItem,
mockShareItem,
Expand Down Expand Up @@ -63,7 +65,9 @@ Cypress.Commands.add(
deleteItemsError = false,
postItemError = false,
moveItemError = false,
moveItemsError = false,
copyItemError = false,
copyItemsError = false,
getItemError = false,
editItemError = false,
shareItemError = false,
Expand Down Expand Up @@ -102,8 +106,12 @@ Cypress.Commands.add(

mockMoveItem(cachedItems, moveItemError);

mockMoveItems(cachedItems, moveItemsError);

mockCopyItem(cachedItems, copyItemError);

mockCopyItems(cachedItems, copyItemsError);

mockEditItem(cachedItems, editItemError);

mockShareItem(cachedItems, shareItemError);
Expand Down
70 changes: 70 additions & 0 deletions cypress/support/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,41 @@ export const mockMoveItem = (items, shouldThrowError) => {
).as('moveItem');
};

export const mockMoveItems = (items, shouldThrowError) => {
cy.intercept(
{
method: DEFAULT_POST.method,
url: new RegExp(`${API_HOST}/items/move\\?id\\=`),
},
({ url, reply, body }) => {
if (shouldThrowError) {
return reply({ statusCode: StatusCodes.BAD_REQUEST, body: null });
}

const ids = url.slice(API_HOST.length).split('=').splice(1);

const updated = ids.map(id => getItemById(items, id));
// actually update cached items
for(const item of updated){
let path = transformIdForPath(item.id);
if (body.parentId) {
const parentItem = getItemById(items, body.parentId);
path = `${parentItem.path}.${path}`;
}
item.path = path;
}

// todo: do for all children

return reply({
statusCode: StatusCodes.OK,
body: updated, // this might not be accurate
});
},
).as('moveItems');
};


export const mockCopyItem = (items, shouldThrowError) => {
cy.intercept(
{
Expand Down Expand Up @@ -405,6 +440,41 @@ export const mockCopyItem = (items, shouldThrowError) => {
).as('copyItem');
};

export const mockCopyItems = (items, shouldThrowError) => {
cy.intercept(
{
method: DEFAULT_POST.method,
url: new RegExp(`${API_HOST}/items/copy\\?id\\=`),
},
({ url, reply, body }) => {
if (shouldThrowError) {
return reply({ statusCode: StatusCodes.BAD_REQUEST, body: null });
}
const ids = url.slice(API_HOST.length).split('=').splice(1);

const original = ids.map(id => getItemById(items, id));
const copies = [];
for(const item of original){
const newId = uuidv4();
// actually copy
let path = transformIdForPath(newId);
if (body.parentId) {
const parentItem = getItemById(items, body.parentId);
path = `${parentItem.path}.${path}`;
}
const newItem = { ...item, id: newId, path };
items.push(newItem);
copies.push(newItem)
}
// todo: do for all children
return reply({
statusCode: StatusCodes.OK,
body: copies,
});
},
).as('copyItems');
};

export const mockEditItem = (items, shouldThrowError) => {
cy.intercept(
{
Expand Down

0 comments on commit 38fef57

Please sign in to comment.