From d797b614da7a7b8b8f759783b1dd44bb461ecf45 Mon Sep 17 00:00:00 2001 From: pyphilia Date: Tue, 13 Jul 2021 14:04:54 +0200 Subject: [PATCH] fix: on error favorite items shows header and mainmenu --- .../item/favorite/favoriteItem.spec.js | 104 +++++++++++------ cypress/support/commands.js | 3 + cypress/support/server.js | 34 ++++++ package.json | 2 +- src/components/main/FavoriteItems.js | 26 +++-- yarn.lock | 109 +++++++++--------- 6 files changed, 181 insertions(+), 97 deletions(-) diff --git a/cypress/integration/item/favorite/favoriteItem.spec.js b/cypress/integration/item/favorite/favoriteItem.spec.js index c61b2193e..6f6848a48 100644 --- a/cypress/integration/item/favorite/favoriteItem.spec.js +++ b/cypress/integration/item/favorite/favoriteItem.spec.js @@ -3,6 +3,7 @@ import { FAVORITE_ITEMS_PATH, HOME_PATH } from '../../../../src/config/paths'; import { buildItemsTableRowId, FAVORITE_ITEM_BUTTON_CLASS, + FAVORITE_ITEMS_ERROR_ALERT_ID, } from '../../../../src/config/selectors'; import { buildMemberWithFavorites } from '../../../fixtures/members'; import { TABLE_ITEM_RENDER_TIME } from '../../../support/constants'; @@ -14,54 +15,89 @@ const toggleFavoriteButton = (itemId) => { ).click(); }; -const favoriteItems = [SAMPLE_ITEMS.items[1].id]; +const favoriteItems = [SAMPLE_ITEMS.items[1].id, SAMPLE_ITEMS.items[2].id]; describe('Favorite Item', () => { - beforeEach(() => { - cy.setUpApi({ - ...SAMPLE_ITEMS, - currentMember: buildMemberWithFavorites(favoriteItems), + describe('Member has several valid favorite items', () => { + beforeEach(() => { + cy.setUpApi({ + ...SAMPLE_ITEMS, + currentMember: buildMemberWithFavorites(favoriteItems), + }); + cy.visit(HOME_PATH); }); - cy.visit(HOME_PATH); - }); - it('add item to favorites', () => { - const item = SAMPLE_ITEMS.items[0]; + it('add item to favorites', () => { + const item = SAMPLE_ITEMS.items[0]; - toggleFavoriteButton(item.id); + toggleFavoriteButton(item.id); - cy.wait('@editMember').then( - ({ - request: { - body: { extra }, + cy.wait('@editMember').then( + ({ + request: { + body: { extra }, + }, + }) => { + expect(extra.favoriteItems.includes(item.id)); }, - }) => { - expect(extra.favoriteItems.includes(item.id)); - }, - ); - }); + ); + }); - it('remove item from favorites', () => { - const itemId = favoriteItems[0]; + it('remove item from favorites', () => { + const itemId = favoriteItems[0]; - toggleFavoriteButton(itemId); + toggleFavoriteButton(itemId); - cy.wait('@editMember').then( - ({ - request: { - body: { extra }, + cy.wait('@editMember').then( + ({ + request: { + body: { extra }, + }, + }) => { + expect(!extra.favoriteItems.includes(itemId)); }, - }) => { - expect(!extra.favoriteItems.includes(itemId)); - }, - ); + ); + }); + + it('check favorite items view', () => { + cy.visit(FAVORITE_ITEMS_PATH); + + const itemId = favoriteItems[0]; + + cy.get(`#${buildItemsTableRowId(itemId)}`).should('exist'); + }); }); - it('check favorite items view', () => { - cy.visit(FAVORITE_ITEMS_PATH); + describe('Errors handling', () => { + it('check favorite items view with one deleted item', () => { + const itemId = 'non existing id'; + cy.setUpApi({ + ...SAMPLE_ITEMS, + currentMember: buildMemberWithFavorites([itemId]), + }); + cy.visit(FAVORITE_ITEMS_PATH); + + cy.get(`#${FAVORITE_ITEMS_ERROR_ALERT_ID}`).should('exist'); + }); - const itemId = favoriteItems[0]; + it('check favorite items view with multiple deleted item', () => { + const itemId = 'ecafbd2a-5688-11eb-ae93-2212bc437002'; + cy.setUpApi({ + ...SAMPLE_ITEMS, + currentMember: buildMemberWithFavorites([itemId, favoriteItems[0]]), + }); + cy.visit(FAVORITE_ITEMS_PATH); - cy.get(`#${buildItemsTableRowId(itemId)}`).should('exist'); + // delete non existing id automatically + cy.wait('@editMember').then( + ({ + request: { + body: { extra }, + }, + }) => { + expect(!extra.favoriteItems.includes(itemId)); + }, + ); + }); }); }); diff --git a/cypress/support/commands.js b/cypress/support/commands.js index c7c2d1913..3d74bb73a 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -40,6 +40,7 @@ import { mockDeleteItemMembershipForItem, mockGetPublicItem, mockGetPublicChildren, + mockGetItems, } from './server'; import './commands/item'; import './commands/navigation'; @@ -140,6 +141,8 @@ Cypress.Commands.add( mockGetPublicItem(items); mockGetPublicChildren(items); + + mockGetItems({ items, currentMember }); }, ); diff --git a/cypress/support/server.js b/cypress/support/server.js index 5b8aa02e1..ec515e311 100644 --- a/cypress/support/server.js +++ b/cypress/support/server.js @@ -269,6 +269,40 @@ export const mockGetPublicItem = (items) => { ).as('getPublicItem'); }; +export const mockGetItems = ({ items, currentMember }, shouldThrowError) => { + cy.intercept( + { + method: DEFAULT_GET.method, + url: new RegExp(`${API_HOST}/items\\?id\\=`), + }, + ({ url, reply }) => { + const { id: itemIds } = qs.parse(url.slice(url.indexOf('?') + 1)); + return reply( + itemIds.map((id) => { + const item = getItemById(items, id); + + // mock membership + const creator = item?.creator; + const haveMembership = + creator === currentMember.id || + item?.memberships?.find( + ({ memberId }) => memberId === currentMember.id, + ); + if (shouldThrowError || !haveMembership) { + return { statusCode: StatusCodes.UNAUTHORIZED, body: null }; + } + + return ( + item || { + statusCode: StatusCodes.NOT_FOUND, + } + ); + }), + ); + }, + ).as('getItems'); +}; + export const mockGetChildren = ({ items, currentMember }) => { cy.intercept( { diff --git a/package.json b/package.json index 1f42b2c05..e31d37f0d 100644 --- a/package.json +++ b/package.json @@ -63,7 +63,7 @@ "hooks:install": "node node_modules/husky/husky.js install", "cypress:open": "env-cmd -f ./.env.test cypress open", "cypress": "concurrently \"yarn start:test\" \"wait-on http://localhost:3111 && yarn cypress:run\"", - "cypress:run": "env-cmd -f ./.env.test cypress run --headless --browser chrome --spec \"cypress/**/*.spec.js\"" + "cypress:run": "env-cmd -f ./.env.test cypress run --headless --browser chrome --spec \"cypress/**/editFile.spec.js\"" }, "eslintConfig": { "extends": "react-app" diff --git a/src/components/main/FavoriteItems.js b/src/components/main/FavoriteItems.js index e81b24435..c02b34faf 100644 --- a/src/components/main/FavoriteItems.js +++ b/src/components/main/FavoriteItems.js @@ -35,6 +35,7 @@ const FavoriteItems = () => { const mutation = useMutation(MUTATION_KEYS.EDIT_MEMBER); // Whenever we have a change in the favorite items, we check for any deleted items and remove them + // this effect does not take effect if there is only one (deleted) item useEffect(() => { if (!favoriteItems.isEmpty() && containsNonExistingItems(favoriteItems)) { const errorIds = getErrorItemIds(favoriteItems); @@ -50,22 +51,27 @@ const FavoriteItems = () => { // eslint-disable-next-line react-hooks/exhaustive-deps }, [favoriteItems]); - if (isMemberError || isItemsError) { - return ; - } - - if (isMemberLoading || isItemsLoading) { - return ; - } + const renderContent = () => { + if (isMemberError || isItemsError) { + return ; + } - return ( -
- + if (isMemberLoading || isItemsLoading) { + return ; + } + return ( + ); + }; + + return ( +
+ + {renderContent()}
); }; diff --git a/yarn.lock b/yarn.lock index 375d47f58..1fd5d0771 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2500,27 +2500,27 @@ "@types/yargs-parser" "*" "@typescript-eslint/eslint-plugin@^4.5.0": - version "4.28.4" - resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-4.28.4.tgz#e73c8cabbf3f08dee0e1bda65ed4e622ae8f8921" - integrity sha512-s1oY4RmYDlWMlcV0kKPBaADn46JirZzvvH7c2CtAqxCY96S538JRBAzt83RrfkDheV/+G/vWNK0zek+8TB3Gmw== + version "4.28.5" + resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-4.28.5.tgz#8197f1473e7da8218c6a37ff308d695707835684" + integrity sha512-m31cPEnbuCqXtEZQJOXAHsHvtoDi9OVaeL5wZnO2KZTnkvELk+u6J6jHg+NzvWQxk+87Zjbc4lJS4NHmgImz6Q== dependencies: - "@typescript-eslint/experimental-utils" "4.28.4" - "@typescript-eslint/scope-manager" "4.28.4" + "@typescript-eslint/experimental-utils" "4.28.5" + "@typescript-eslint/scope-manager" "4.28.5" debug "^4.3.1" functional-red-black-tree "^1.0.1" regexpp "^3.1.0" semver "^7.3.5" tsutils "^3.21.0" -"@typescript-eslint/experimental-utils@4.28.4", "@typescript-eslint/experimental-utils@^4.0.1": - version "4.28.4" - resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-4.28.4.tgz#9c70c35ebed087a5c70fb0ecd90979547b7fec96" - integrity sha512-OglKWOQRWTCoqMSy6pm/kpinEIgdcXYceIcH3EKWUl4S8xhFtN34GQRaAvTIZB9DD94rW7d/U7tUg3SYeDFNHA== +"@typescript-eslint/experimental-utils@4.28.5", "@typescript-eslint/experimental-utils@^4.0.1": + version "4.28.5" + resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-4.28.5.tgz#66c28bef115b417cf9d80812a713e0e46bb42a64" + integrity sha512-bGPLCOJAa+j49hsynTaAtQIWg6uZd8VLiPcyDe4QPULsvQwLHGLSGKKcBN8/lBxIX14F74UEMK2zNDI8r0okwA== dependencies: "@types/json-schema" "^7.0.7" - "@typescript-eslint/scope-manager" "4.28.4" - "@typescript-eslint/types" "4.28.4" - "@typescript-eslint/typescript-estree" "4.28.4" + "@typescript-eslint/scope-manager" "4.28.5" + "@typescript-eslint/types" "4.28.5" + "@typescript-eslint/typescript-estree" "4.28.5" eslint-scope "^5.1.1" eslint-utils "^3.0.0" @@ -2536,32 +2536,32 @@ eslint-utils "^2.0.0" "@typescript-eslint/parser@^4.5.0": - version "4.28.4" - resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-4.28.4.tgz#bc462dc2779afeefdcf49082516afdc3e7b96fab" - integrity sha512-4i0jq3C6n+og7/uCHiE6q5ssw87zVdpUj1k6VlVYMonE3ILdFApEzTWgppSRG4kVNB/5jxnH+gTeKLMNfUelQA== + version "4.28.5" + resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-4.28.5.tgz#9c971668f86d1b5c552266c47788a87488a47d1c" + integrity sha512-NPCOGhTnkXGMqTznqgVbA5LqVsnw+i3+XA1UKLnAb+MG1Y1rP4ZSK9GX0kJBmAZTMIktf+dTwXToT6kFwyimbw== dependencies: - "@typescript-eslint/scope-manager" "4.28.4" - "@typescript-eslint/types" "4.28.4" - "@typescript-eslint/typescript-estree" "4.28.4" + "@typescript-eslint/scope-manager" "4.28.5" + "@typescript-eslint/types" "4.28.5" + "@typescript-eslint/typescript-estree" "4.28.5" debug "^4.3.1" -"@typescript-eslint/scope-manager@4.28.4": - version "4.28.4" - resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-4.28.4.tgz#bdbce9b6a644e34f767bd68bc17bb14353b9fe7f" - integrity sha512-ZJBNs4usViOmlyFMt9X9l+X0WAFcDH7EdSArGqpldXu7aeZxDAuAzHiMAeI+JpSefY2INHrXeqnha39FVqXb8w== +"@typescript-eslint/scope-manager@4.28.5": + version "4.28.5" + resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-4.28.5.tgz#3a1b70c50c1535ac33322786ea99ebe403d3b923" + integrity sha512-PHLq6n9nTMrLYcVcIZ7v0VY1X7dK309NM8ya9oL/yG8syFINIMHxyr2GzGoBYUdv3NUfCOqtuqps0ZmcgnZTfQ== dependencies: - "@typescript-eslint/types" "4.28.4" - "@typescript-eslint/visitor-keys" "4.28.4" + "@typescript-eslint/types" "4.28.5" + "@typescript-eslint/visitor-keys" "4.28.5" "@typescript-eslint/types@3.10.1": version "3.10.1" resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-3.10.1.tgz#1d7463fa7c32d8a23ab508a803ca2fe26e758727" integrity sha512-+3+FCUJIahE9q0lDi1WleYzjCwJs5hIsbugIgnbB+dSCYUxl8L6PwmsyOPFZde2hc1DlTo/xnkOgiTLSyAbHiQ== -"@typescript-eslint/types@4.28.4": - version "4.28.4" - resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.28.4.tgz#41acbd79b5816b7c0dd7530a43d97d020d3aeb42" - integrity sha512-3eap4QWxGqkYuEmVebUGULMskR6Cuoc/Wii0oSOddleP4EGx1tjLnZQ0ZP33YRoMDCs5O3j56RBV4g14T4jvww== +"@typescript-eslint/types@4.28.5": + version "4.28.5" + resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.28.5.tgz#d33edf8e429f0c0930a7c3d44e9b010354c422e9" + integrity sha512-MruOu4ZaDOLOhw4f/6iudyks/obuvvZUAHBDSW80Trnc5+ovmViLT2ZMDXhUV66ozcl6z0LJfKs1Usldgi/WCA== "@typescript-eslint/typescript-estree@3.10.1": version "3.10.1" @@ -2577,13 +2577,13 @@ semver "^7.3.2" tsutils "^3.17.1" -"@typescript-eslint/typescript-estree@4.28.4": - version "4.28.4" - resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-4.28.4.tgz#252e6863278dc0727244be9e371eb35241c46d00" - integrity sha512-z7d8HK8XvCRyN2SNp+OXC2iZaF+O2BTquGhEYLKLx5k6p0r05ureUtgEfo5f6anLkhCxdHtCf6rPM1p4efHYDQ== +"@typescript-eslint/typescript-estree@4.28.5": + version "4.28.5" + resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-4.28.5.tgz#4906d343de693cf3d8dcc301383ed638e0441cd1" + integrity sha512-FzJUKsBX8poCCdve7iV7ShirP8V+ys2t1fvamVeD1rWpiAnIm550a+BX/fmTHrjEpQJ7ZAn+Z7ZZwJjytk9rZw== dependencies: - "@typescript-eslint/types" "4.28.4" - "@typescript-eslint/visitor-keys" "4.28.4" + "@typescript-eslint/types" "4.28.5" + "@typescript-eslint/visitor-keys" "4.28.5" debug "^4.3.1" globby "^11.0.3" is-glob "^4.0.1" @@ -2597,12 +2597,12 @@ dependencies: eslint-visitor-keys "^1.1.0" -"@typescript-eslint/visitor-keys@4.28.4": - version "4.28.4" - resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-4.28.4.tgz#92dacfefccd6751cbb0a964f06683bfd72d0c4d3" - integrity sha512-NIAXAdbz1XdOuzqkJHjNKXKj8QQ4cv5cxR/g0uQhCYf/6//XrmfpaYsM7PnBcNbfvTDLUkqQ5TPNm1sozDdTWg== +"@typescript-eslint/visitor-keys@4.28.5": + version "4.28.5" + resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-4.28.5.tgz#ffee2c602762ed6893405ee7c1144d9cc0a29675" + integrity sha512-dva/7Rr+EkxNWdJWau26xU/0slnFlkh88v3TsyTgRS/IIYFi5iIfpCFM4ikw0vQTFUR9FYSSyqgK4w64gsgxhg== dependencies: - "@typescript-eslint/types" "4.28.4" + "@typescript-eslint/types" "4.28.5" eslint-visitor-keys "^2.0.0" "@uppy/aws-s3@1.7.7": @@ -6073,9 +6073,9 @@ ejs@^2.6.1: integrity sha512-7vmuyh5+kuUyJKePhQfRQBhXV5Ce+RnaeeQArKu1EAMpL3WbgMt5WG6uQZpEVvYSSsxMXRKOewtDk9RaTKXRlA== electron-to-chromium@^1.3.564, electron-to-chromium@^1.3.723: - version "1.3.786" - resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.786.tgz#1fc572abc77e2f474725f8a61acf7e25ced9fbe2" - integrity sha512-AmvbLBj3hepRk8v/DHrFF8gINxOFfDbrn6Ts3PcK46/FBdQb5OMmpamSpZQXSkfi77FfBzYtQtAk+00LCLYMVw== + version "1.3.788" + resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.788.tgz#7a304c8ebb11d30916a1a1c1b4a9bad3983ef232" + integrity sha512-dbMIpX4E4/Gk4gzOh1GYS7ls1vGsByWKpIqLviJi1mSmSt5BvrWLLtSqpFE5BaC7Ef4NnI0GMaiddNX2Brw6zA== elegant-spinner@^1.0.1: version "1.0.1" @@ -6333,9 +6333,9 @@ eslint-module-utils@^2.6.0, eslint-module-utils@^2.6.1: pkg-dir "^2.0.0" eslint-plugin-flowtype@^5.2.0: - version "5.8.1" - resolved "https://registry.yarnpkg.com/eslint-plugin-flowtype/-/eslint-plugin-flowtype-5.8.1.tgz#6f2c313a6e9ec271a51315ccd09c2432b4d1930e" - integrity sha512-ADe4cYAn1UYVs1P1F9wKDNP2lbm3JtwbFykxJpO129utErjAhRZYQJCIKmhy6Rpx7Q9p8pc3gLVIFXtatXJriQ== + version "5.8.2" + resolved "https://registry.yarnpkg.com/eslint-plugin-flowtype/-/eslint-plugin-flowtype-5.8.2.tgz#e8c4429f2d39d93ccb0732f581b159da16fa0bda" + integrity sha512-/aPTnNKNAYJbEU07HwvohnXp0itBT+P0r+7s80IG5eqfsrx4NLN+0rXNztJBc56u1RJegSn0GMt1cZnGZpCThw== dependencies: lodash "^4.17.15" string-natural-compare "^3.0.1" @@ -8574,9 +8574,9 @@ is-stream@^1.1.0: integrity sha1-EtSj3U5o4Lec6428hBc66A2RykQ= is-stream@^2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-2.0.0.tgz#bde9c32680d6fae04129d6ac9d921ce7815f78e3" - integrity sha512-XCoy+WlUr7d1+Z8GgSuXmpuUFC9fOhRXglJMx+dwLKTkL44Cjd4W1Z5P+BQZpr+cR93aGP4S/s7Ftw6Nd/kiEw== + version "2.0.1" + resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-2.0.1.tgz#fac1e3d53b97ad5a9d0ae9cef2389f5810a5c077" + integrity sha512-hFoiJiTl63nn+kstHGBtewWSKnQLpyb155KHheA1l39uvtO9nWIop1p3udqPcUd/xbF1VLMO4n7OI6p7RbngDg== is-string@^1.0.5, is-string@^1.0.6: version "1.0.6" @@ -10096,11 +10096,16 @@ miller-rabin@^4.0.0: bn.js "^4.0.0" brorand "^1.0.1" -mime-db@1.48.0, "mime-db@>= 1.43.0 < 2": +mime-db@1.48.0: version "1.48.0" resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.48.0.tgz#e35b31045dd7eada3aaad537ed88a33afbef2d1d" integrity sha512-FM3QwxV+TnZYQ2aRqhlKBMHxk10lTbMt3bBkMAp54ddrNeVSfcQYOOKuGuy3Ddrm38I04If834fOUSq1yzslJQ== +"mime-db@>= 1.43.0 < 2": + version "1.49.0" + resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.49.0.tgz#f3dfde60c99e9cf3bc9701d687778f537001cbed" + integrity sha512-CIc8j9URtOVApSFCQIF+VBkX1RwXp/oMMOrqdyXSBXq5RWNEsRfyj1kiRnQgmNXmHxPoFIxOroKA3zcU9P+nAA== + mime-match@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/mime-match/-/mime-match-1.0.2.tgz#3f87c31e9af1a5fd485fb9db134428b23bbb7ba8" @@ -14253,9 +14258,9 @@ tapable@^1.0.0, tapable@^1.1.3: integrity sha512-4WK/bYZmj8xLr+HUCODHGF1ZFzsYffasLUgEiMBY4fgtltdO6B4WJtlSbPaDTLpYTcGVwM2qLnFTICEcNxs3kA== tar@^6.0.2: - version "6.1.1" - resolved "https://registry.yarnpkg.com/tar/-/tar-6.1.1.tgz#4d7da4b132b334bb8c175ed1de466fe9157ea0eb" - integrity sha512-GG0R7yt/CQkvG4fueXDi52Zskqxe2AyRJ+Wm54yqarnBgcX3qRIWh10qLVAAN+mlPFGTfP5UxvD3Fbi11UOTUQ== + version "6.1.2" + resolved "https://registry.yarnpkg.com/tar/-/tar-6.1.2.tgz#1f045a90a6eb23557a603595f41a16c57d47adc6" + integrity sha512-EwKEgqJ7nJoS+s8QfLYVGMDmAsj+StbI2AM/RTHeUSsOw6Z8bwNBRv5z3CY0m7laC5qUAqruLX5AhMuc5deY3Q== dependencies: chownr "^2.0.0" fs-minipass "^2.0.0"