Created
March 28, 2023 07:47
-
-
Save kianaditya/0578363724be788844497b6692699cfd to your computer and use it in GitHub Desktop.
Test scopes using jest tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"$schema": "https://aka.ms/codetour-schema", | |
"title": "Testing scopes using jest", | |
"steps": [ | |
{ | |
"file": "src/components/MaterialStandard/materialStandard.scope.js", | |
"description": "We first build an array of objects, where each object contains information about setting up the test, and an assertion. \n\nAssertions included in this structure are - `inDocument`, `notInDocument`, and `calls` ( that tracks no of calls to an api)\n\nA standard structure for a \"test object\" is as follows:\n```js\n{\nname: \"<denotes name of the test that shows up when we run it>\"\ncomponent:<component under test>\nscope:<scope array takes in all the applicable scopes for testing>\nroute:<which route we should render the component with>\nassertion: <one of three assertions writtern as screen => screen ...>\nmockedRoute: <if we are asserting no of calls made to particular endpoint>\ncalls: <asserting no of calls>\n}\n```", | |
"line": 22, | |
"contents": "import React from 'react'\nimport * as materialStandardApi from 'src/api/materialStandardApi'\nimport * as periodicElementsApi from 'src/api/periodicApi'\nimport View from 'src/components/MaterialStandard/View'\nimport { UserScope } from 'src/typings/generated/types'\n\njest.spyOn(materialStandardApi, 'fetchMaterialStandard').mockReturnValue({\n id: '39a6c908-2aa9-40ce-9a8a-a59c7fbe9da0',\n versionId: 'c22f00fb-2289-448c-9588-ae3a4016291b',\n companyId: '1',\n company: { id: '1', name: 'Company #1' },\n name: 'New Material Standard',\n description: 'New Material standard description',\n global: false,\n createdAt: '2021-10-25T09:26:25.454Z',\n createdBy: { id: '11111111-2222-3333-4444-555555555555', name: 'Test First' },\n substances: [{ id: '94553ce0-ce04-4d7f-98f5-62c9ff31e9dd', element: 'H', min: '800', max: '1000', optional: false }],\n})\n\njest.spyOn(periodicElementsApi, 'fetchElements').mockReturnValue({})\n\nconst test = [\n {\n name: 'Material Standard Assert delete button not in document',\n component: <View />,\n scope: [{ companyId: '1', scope: UserScope.ScopeMaterialStandardsRead }],\n route: ['/material-standard/39a6c908-2aa9-40ce-9a8a-a59c7fbe9da0'],\n notInDocument: screen => screen.queryByTestId('delete-material-standard-button'),\n },\n {\n name: 'Material Standard Assert delete button in document',\n component: <View />,\n scope: [\n { companyId: '1', scope: UserScope.ScopeMaterialStandardsRead },\n { companyId: '1', scope: UserScope.ScopeMaterialStandardsDelete },\n ],\n route: ['/material-standard/39a6c908-2aa9-40ce-9a8a-a59c7fbe9da0'],\n inDocument: screen => screen.queryByTestId('delete-material-standard-button'),\n },\n]\n\nexport default [{ title: 'Material Standard', list: test }]\n" | |
}, | |
{ | |
"file": "src/components/MaterialStandard/materialStandard.scope.js", | |
"description": "We then export the array of test objects with a title, this title will reflect on `describe` part of the test.", | |
"line": 42, | |
"contents": "import React from 'react'\nimport * as materialStandardApi from 'src/api/materialStandardApi'\nimport * as periodicElementsApi from 'src/api/periodicApi'\nimport View from 'src/components/MaterialStandard/View'\nimport { UserScope } from 'src/typings/generated/types'\n\njest.spyOn(materialStandardApi, 'fetchMaterialStandard').mockReturnValue({\n id: '39a6c908-2aa9-40ce-9a8a-a59c7fbe9da0',\n versionId: 'c22f00fb-2289-448c-9588-ae3a4016291b',\n companyId: '1',\n company: { id: '1', name: 'Company #1' },\n name: 'New Material Standard',\n description: 'New Material standard description',\n global: false,\n createdAt: '2021-10-25T09:26:25.454Z',\n createdBy: { id: '11111111-2222-3333-4444-555555555555', name: 'Test First' },\n substances: [{ id: '94553ce0-ce04-4d7f-98f5-62c9ff31e9dd', element: 'H', min: '800', max: '1000', optional: false }],\n})\n\njest.spyOn(periodicElementsApi, 'fetchElements').mockReturnValue({})\n\nconst test = [\n {\n name: 'Material Standard Assert delete button not in document',\n component: <View />,\n scope: [{ companyId: '1', scope: UserScope.ScopeMaterialStandardsRead }],\n route: ['/material-standard/39a6c908-2aa9-40ce-9a8a-a59c7fbe9da0'],\n notInDocument: screen => screen.queryByTestId('delete-material-standard-button'),\n },\n {\n name: 'Material Standard Assert delete button in document',\n component: <View />,\n scope: [\n { companyId: '1', scope: UserScope.ScopeMaterialStandardsRead },\n { companyId: '1', scope: UserScope.ScopeMaterialStandardsDelete },\n ],\n route: ['/material-standard/39a6c908-2aa9-40ce-9a8a-a59c7fbe9da0'],\n inDocument: screen => screen.queryByTestId('delete-material-standard-button'),\n },\n]\n\nexport default [{ title: 'Material Standard', list: test }]\n" | |
}, | |
{ | |
"file": "src/__TESTS__/Lists/Scopes.test.js", | |
"description": "This `scopes.test.js` file is where we loop through all the arrays of test objects we have created and assert the required conditions.", | |
"line": 9, | |
"contents": "import { waitFor } from '@testing-library/react'\nimport { screen, render } from 'test-utils'\nimport inviteUsersTestList from 'src/components/Admin/admin.scopes'\nimport classStandardList from 'src/components/ClassStandard/classStandard.scope'\nimport counterpartyTestList from 'src/components/Counterparty/counterparty.scopes'\nimport sideMenuTestList from 'src/components/Interface/SideBar/sideMenu.scopes'\nimport materialStandardList from 'src/components/MaterialStandard/materialStandard.scope'\n\n// In this file we are testing the scopes of the different components in the app to make sure that the correct buttons/components are displayed for the correct scopes and that the correct api calls are made for the correct scopes.\n// The test is done by passing in a list of tests for each component. Each test has a name, a component, a scope, a route, and a function that checks if the correct button/component/text is in the document or not.\n// The test is then run for each test in the list and the correct button/component is checked for each scope.\nconst actor = {\n token: '1111',\n companyId: '1',\n scopes: [],\n settings: {\n displayIncomingInspection: true,\n displayAnalytics: true,\n displayTraceback: true,\n displaySerialNumber: true,\n displayPartApproval: true,\n displayEarlyWarning: true,\n displayEnvironmentalData: true,\n displayDefaultLinkSubstancesResults: true,\n displayDefaultLinkCharacteristicsResults: true,\n },\n company: {\n id: '1',\n subscriptions: [\n {\n startDate: '2020-07-02T09:03:31Z',\n endDate: '2032-07-02T09:03:31Z',\n settings: {\n incomingInspection: true,\n traceback: true,\n serialNumber: true,\n partApproval: true,\n earlyWarning: true,\n environmentalData: true,\n defaultLinkSubstancesResults: true,\n defaultLinkCharacteristicsResults: true,\n },\n },\n ],\n },\n}\n\n// This is the combined list of all the tests for all the components.\nconst testList = [...counterpartyTestList, ...inviteUsersTestList, ...sideMenuTestList, ...materialStandardList, ...classStandardList]\n\n// This function takes a list of tests and a key and returns a list of tests that have the key 'calls'. Then the test is run for each test in the list and the correct button is checked for each scope.\nconst assertCallsList = filterForKey(testList, 'calls')\n\ndescribe.each(assertCallsList)('$title scope test calls', ({ list }) => {\n test.each(list)('$name', async ({ component, scope, route, mockedRoute, calls }) => {\n const initState = { ...actor, scopes: scope }\n render(component, { route: [...route], store: { me: initState } })\n await waitFor(() => {\n expect(mockedRoute).toHaveBeenCalledTimes(calls)\n })\n })\n})\n\n// This function takes a list of tests and a key and returns a list of tests that have the key 'inDocument'. Then the test is run for each test in the list and the correct button is checked for each scope.\nconst assertInDoc = filterForKey(testList, 'inDocument')\ndescribe.each(assertInDoc)('$title scope test assert', ({ list }) => {\n afterEach(() => {\n jest.clearAllMocks()\n })\n test.each(list)('$name', async ({ component, scope, route, inDocument }) => {\n const initState = { ...actor, scopes: scope }\n render(component, { route: [...route], store: { me: initState } })\n await waitFor(() => {\n expect(inDocument(screen)).toBeInTheDocument()\n })\n })\n})\n\n// This function takes a list of tests and a key and returns a list of tests that have the key 'notInDocument'. Then the test is run for each test in the list and the correct button is checked for each scope.\nconst assertNotInDoc = filterForKey(testList, 'notInDocument')\ndescribe.each(assertNotInDoc)('$title scope test assert', ({ list }) => {\n afterEach(() => {\n jest.clearAllMocks()\n })\n test.each(list)('$name', async ({ component, scope, route, notInDocument }) => {\n const initState = { ...actor, scopes: scope }\n render(component, { route: [...route], store: { me: initState } })\n await waitFor(() => {\n expect(notInDocument(screen)).not.toBeInTheDocument()\n })\n })\n})\n\n// This function takes a list of tests and a key and returns a list of tests that have the key.\nfunction filterForKey(obj, key) {\n const filteredList = obj.map(({ title, list }) => ({ title, list: [...list.filter(obj => obj.hasOwnProperty(key))] }))\n return filteredList.filter(({ list }) => list.length > 0)\n}\n" | |
}, | |
{ | |
"file": "src/__TESTS__/Lists/Scopes.test.js", | |
"description": "We import and combine all test lists previously declared in other parts of our code.", | |
"line": 49, | |
"contents": "import { waitFor } from '@testing-library/react'\nimport { screen, render } from 'test-utils'\nimport inviteUsersTestList from 'src/components/Admin/admin.scopes'\nimport classStandardList from 'src/components/ClassStandard/classStandard.scope'\nimport counterpartyTestList from 'src/components/Counterparty/counterparty.scopes'\nimport sideMenuTestList from 'src/components/Interface/SideBar/sideMenu.scopes'\nimport materialStandardList from 'src/components/MaterialStandard/materialStandard.scope'\n\n// In this file we are testing the scopes of the different components in the app to make sure that the correct buttons/components are displayed for the correct scopes and that the correct api calls are made for the correct scopes.\n// The test is done by passing in a list of tests for each component. Each test has a name, a component, a scope, a route, and a function that checks if the correct button/component/text is in the document or not.\n// The test is then run for each test in the list and the correct button/component is checked for each scope.\nconst actor = {\n token: '1111',\n companyId: '1',\n scopes: [],\n settings: {\n displayIncomingInspection: true,\n displayAnalytics: true,\n displayTraceback: true,\n displaySerialNumber: true,\n displayPartApproval: true,\n displayEarlyWarning: true,\n displayEnvironmentalData: true,\n displayDefaultLinkSubstancesResults: true,\n displayDefaultLinkCharacteristicsResults: true,\n },\n company: {\n id: '1',\n subscriptions: [\n {\n startDate: '2020-07-02T09:03:31Z',\n endDate: '2032-07-02T09:03:31Z',\n settings: {\n incomingInspection: true,\n traceback: true,\n serialNumber: true,\n partApproval: true,\n earlyWarning: true,\n environmentalData: true,\n defaultLinkSubstancesResults: true,\n defaultLinkCharacteristicsResults: true,\n },\n },\n ],\n },\n}\n\n// This is the combined list of all the tests for all the components.\nconst testList = [...counterpartyTestList, ...inviteUsersTestList, ...sideMenuTestList, ...materialStandardList, ...classStandardList]\n\n// This function takes a list of tests and a key and returns a list of tests that have the key 'calls'. Then the test is run for each test in the list and the correct button is checked for each scope.\nconst assertCallsList = filterForKey(testList, 'calls')\n\ndescribe.each(assertCallsList)('$title scope test calls', ({ list }) => {\n test.each(list)('$name', async ({ component, scope, route, mockedRoute, calls }) => {\n const initState = { ...actor, scopes: scope }\n render(component, { route: [...route], store: { me: initState } })\n await waitFor(() => {\n expect(mockedRoute).toHaveBeenCalledTimes(calls)\n })\n })\n})\n\n// This function takes a list of tests and a key and returns a list of tests that have the key 'inDocument'. Then the test is run for each test in the list and the correct button is checked for each scope.\nconst assertInDoc = filterForKey(testList, 'inDocument')\ndescribe.each(assertInDoc)('$title scope test assert', ({ list }) => {\n afterEach(() => {\n jest.clearAllMocks()\n })\n test.each(list)('$name', async ({ component, scope, route, inDocument }) => {\n const initState = { ...actor, scopes: scope }\n render(component, { route: [...route], store: { me: initState } })\n await waitFor(() => {\n expect(inDocument(screen)).toBeInTheDocument()\n })\n })\n})\n\n// This function takes a list of tests and a key and returns a list of tests that have the key 'notInDocument'. Then the test is run for each test in the list and the correct button is checked for each scope.\nconst assertNotInDoc = filterForKey(testList, 'notInDocument')\ndescribe.each(assertNotInDoc)('$title scope test assert', ({ list }) => {\n afterEach(() => {\n jest.clearAllMocks()\n })\n test.each(list)('$name', async ({ component, scope, route, notInDocument }) => {\n const initState = { ...actor, scopes: scope }\n render(component, { route: [...route], store: { me: initState } })\n await waitFor(() => {\n expect(notInDocument(screen)).not.toBeInTheDocument()\n })\n })\n})\n\n// This function takes a list of tests and a key and returns a list of tests that have the key.\nfunction filterForKey(obj, key) {\n const filteredList = obj.map(({ title, list }) => ({ title, list: [...list.filter(obj => obj.hasOwnProperty(key))] }))\n return filteredList.filter(({ list }) => list.length > 0)\n}\n" | |
}, | |
{ | |
"file": "src/__TESTS__/Lists/Scopes.test.js", | |
"description": "We then build a filtered list using specific keys for specific assertions, since each assertion might need different test structure as we can notice in the code.", | |
"line": 52, | |
"contents": "import { waitFor } from '@testing-library/react'\nimport { screen, render } from 'test-utils'\nimport inviteUsersTestList from 'src/components/Admin/admin.scopes'\nimport classStandardList from 'src/components/ClassStandard/classStandard.scope'\nimport counterpartyTestList from 'src/components/Counterparty/counterparty.scopes'\nimport sideMenuTestList from 'src/components/Interface/SideBar/sideMenu.scopes'\nimport materialStandardList from 'src/components/MaterialStandard/materialStandard.scope'\n\n// In this file we are testing the scopes of the different components in the app to make sure that the correct buttons/components are displayed for the correct scopes and that the correct api calls are made for the correct scopes.\n// The test is done by passing in a list of tests for each component. Each test has a name, a component, a scope, a route, and a function that checks if the correct button/component/text is in the document or not.\n// The test is then run for each test in the list and the correct button/component is checked for each scope.\nconst actor = {\n token: '1111',\n companyId: '1',\n scopes: [],\n settings: {\n displayIncomingInspection: true,\n displayAnalytics: true,\n displayTraceback: true,\n displaySerialNumber: true,\n displayPartApproval: true,\n displayEarlyWarning: true,\n displayEnvironmentalData: true,\n displayDefaultLinkSubstancesResults: true,\n displayDefaultLinkCharacteristicsResults: true,\n },\n company: {\n id: '1',\n subscriptions: [\n {\n startDate: '2020-07-02T09:03:31Z',\n endDate: '2032-07-02T09:03:31Z',\n settings: {\n incomingInspection: true,\n traceback: true,\n serialNumber: true,\n partApproval: true,\n earlyWarning: true,\n environmentalData: true,\n defaultLinkSubstancesResults: true,\n defaultLinkCharacteristicsResults: true,\n },\n },\n ],\n },\n}\n\n// This is the combined list of all the tests for all the components.\nconst testList = [...counterpartyTestList, ...inviteUsersTestList, ...sideMenuTestList, ...materialStandardList, ...classStandardList]\n\n// This function takes a list of tests and a key and returns a list of tests that have the key 'calls'. Then the test is run for each test in the list and the correct button is checked for each scope.\nconst assertCallsList = filterForKey(testList, 'calls')\n\ndescribe.each(assertCallsList)('$title scope test calls', ({ list }) => {\n test.each(list)('$name', async ({ component, scope, route, mockedRoute, calls }) => {\n const initState = { ...actor, scopes: scope }\n render(component, { route: [...route], store: { me: initState } })\n await waitFor(() => {\n expect(mockedRoute).toHaveBeenCalledTimes(calls)\n })\n })\n})\n\n// This function takes a list of tests and a key and returns a list of tests that have the key 'inDocument'. Then the test is run for each test in the list and the correct button is checked for each scope.\nconst assertInDoc = filterForKey(testList, 'inDocument')\ndescribe.each(assertInDoc)('$title scope test assert', ({ list }) => {\n afterEach(() => {\n jest.clearAllMocks()\n })\n test.each(list)('$name', async ({ component, scope, route, inDocument }) => {\n const initState = { ...actor, scopes: scope }\n render(component, { route: [...route], store: { me: initState } })\n await waitFor(() => {\n expect(inDocument(screen)).toBeInTheDocument()\n })\n })\n})\n\n// This function takes a list of tests and a key and returns a list of tests that have the key 'notInDocument'. Then the test is run for each test in the list and the correct button is checked for each scope.\nconst assertNotInDoc = filterForKey(testList, 'notInDocument')\ndescribe.each(assertNotInDoc)('$title scope test assert', ({ list }) => {\n afterEach(() => {\n jest.clearAllMocks()\n })\n test.each(list)('$name', async ({ component, scope, route, notInDocument }) => {\n const initState = { ...actor, scopes: scope }\n render(component, { route: [...route], store: { me: initState } })\n await waitFor(() => {\n expect(notInDocument(screen)).not.toBeInTheDocument()\n })\n })\n})\n\n// This function takes a list of tests and a key and returns a list of tests that have the key.\nfunction filterForKey(obj, key) {\n const filteredList = obj.map(({ title, list }) => ({ title, list: [...list.filter(obj => obj.hasOwnProperty(key))] }))\n return filteredList.filter(({ list }) => list.length > 0)\n}\n" | |
}, | |
{ | |
"file": "src/__TESTS__/Lists/Scopes.test.js", | |
"description": "Here, for example, we are asserting number of calls made to mockedRoute.", | |
"line": 59, | |
"contents": "import { waitFor } from '@testing-library/react'\nimport { screen, render } from 'test-utils'\nimport inviteUsersTestList from 'src/components/Admin/admin.scopes'\nimport classStandardList from 'src/components/ClassStandard/classStandard.scope'\nimport counterpartyTestList from 'src/components/Counterparty/counterparty.scopes'\nimport sideMenuTestList from 'src/components/Interface/SideBar/sideMenu.scopes'\nimport materialStandardList from 'src/components/MaterialStandard/materialStandard.scope'\n\n// In this file we are testing the scopes of the different components in the app to make sure that the correct buttons/components are displayed for the correct scopes and that the correct api calls are made for the correct scopes.\n// The test is done by passing in a list of tests for each component. Each test has a name, a component, a scope, a route, and a function that checks if the correct button/component/text is in the document or not.\n// The test is then run for each test in the list and the correct button/component is checked for each scope.\nconst actor = {\n token: '1111',\n companyId: '1',\n scopes: [],\n settings: {\n displayIncomingInspection: true,\n displayAnalytics: true,\n displayTraceback: true,\n displaySerialNumber: true,\n displayPartApproval: true,\n displayEarlyWarning: true,\n displayEnvironmentalData: true,\n displayDefaultLinkSubstancesResults: true,\n displayDefaultLinkCharacteristicsResults: true,\n },\n company: {\n id: '1',\n subscriptions: [\n {\n startDate: '2020-07-02T09:03:31Z',\n endDate: '2032-07-02T09:03:31Z',\n settings: {\n incomingInspection: true,\n traceback: true,\n serialNumber: true,\n partApproval: true,\n earlyWarning: true,\n environmentalData: true,\n defaultLinkSubstancesResults: true,\n defaultLinkCharacteristicsResults: true,\n },\n },\n ],\n },\n}\n\n// This is the combined list of all the tests for all the components.\nconst testList = [...counterpartyTestList, ...inviteUsersTestList, ...sideMenuTestList, ...materialStandardList, ...classStandardList]\n\n// This function takes a list of tests and a key and returns a list of tests that have the key 'calls'. Then the test is run for each test in the list and the correct button is checked for each scope.\nconst assertCallsList = filterForKey(testList, 'calls')\n\ndescribe.each(assertCallsList)('$title scope test calls', ({ list }) => {\n test.each(list)('$name', async ({ component, scope, route, mockedRoute, calls }) => {\n const initState = { ...actor, scopes: scope }\n render(component, { route: [...route], store: { me: initState } })\n await waitFor(() => {\n expect(mockedRoute).toHaveBeenCalledTimes(calls)\n })\n })\n})\n\n// This function takes a list of tests and a key and returns a list of tests that have the key 'inDocument'. Then the test is run for each test in the list and the correct button is checked for each scope.\nconst assertInDoc = filterForKey(testList, 'inDocument')\ndescribe.each(assertInDoc)('$title scope test assert', ({ list }) => {\n afterEach(() => {\n jest.clearAllMocks()\n })\n test.each(list)('$name', async ({ component, scope, route, inDocument }) => {\n const initState = { ...actor, scopes: scope }\n render(component, { route: [...route], store: { me: initState } })\n await waitFor(() => {\n expect(inDocument(screen)).toBeInTheDocument()\n })\n })\n})\n\n// This function takes a list of tests and a key and returns a list of tests that have the key 'notInDocument'. Then the test is run for each test in the list and the correct button is checked for each scope.\nconst assertNotInDoc = filterForKey(testList, 'notInDocument')\ndescribe.each(assertNotInDoc)('$title scope test assert', ({ list }) => {\n afterEach(() => {\n jest.clearAllMocks()\n })\n test.each(list)('$name', async ({ component, scope, route, notInDocument }) => {\n const initState = { ...actor, scopes: scope }\n render(component, { route: [...route], store: { me: initState } })\n await waitFor(() => {\n expect(notInDocument(screen)).not.toBeInTheDocument()\n })\n })\n})\n\n// This function takes a list of tests and a key and returns a list of tests that have the key.\nfunction filterForKey(obj, key) {\n const filteredList = obj.map(({ title, list }) => ({ title, list: [...list.filter(obj => obj.hasOwnProperty(key))] }))\n return filteredList.filter(({ list }) => list.length > 0)\n}\n" | |
}, | |
{ | |
"file": "src/__TESTS__/Lists/Scopes.test.js", | |
"description": "We also assert `inDocument` and `notInDocument` assertions.", | |
"line": 65, | |
"contents": "import { waitFor } from '@testing-library/react'\nimport { screen, render } from 'test-utils'\nimport inviteUsersTestList from 'src/components/Admin/admin.scopes'\nimport classStandardList from 'src/components/ClassStandard/classStandard.scope'\nimport counterpartyTestList from 'src/components/Counterparty/counterparty.scopes'\nimport sideMenuTestList from 'src/components/Interface/SideBar/sideMenu.scopes'\nimport materialStandardList from 'src/components/MaterialStandard/materialStandard.scope'\n\n// In this file we are testing the scopes of the different components in the app to make sure that the correct buttons/components are displayed for the correct scopes and that the correct api calls are made for the correct scopes.\n// The test is done by passing in a list of tests for each component. Each test has a name, a component, a scope, a route, and a function that checks if the correct button/component/text is in the document or not.\n// The test is then run for each test in the list and the correct button/component is checked for each scope.\nconst actor = {\n token: '1111',\n companyId: '1',\n scopes: [],\n settings: {\n displayIncomingInspection: true,\n displayAnalytics: true,\n displayTraceback: true,\n displaySerialNumber: true,\n displayPartApproval: true,\n displayEarlyWarning: true,\n displayEnvironmentalData: true,\n displayDefaultLinkSubstancesResults: true,\n displayDefaultLinkCharacteristicsResults: true,\n },\n company: {\n id: '1',\n subscriptions: [\n {\n startDate: '2020-07-02T09:03:31Z',\n endDate: '2032-07-02T09:03:31Z',\n settings: {\n incomingInspection: true,\n traceback: true,\n serialNumber: true,\n partApproval: true,\n earlyWarning: true,\n environmentalData: true,\n defaultLinkSubstancesResults: true,\n defaultLinkCharacteristicsResults: true,\n },\n },\n ],\n },\n}\n\n// This is the combined list of all the tests for all the components.\nconst testList = [...counterpartyTestList, ...inviteUsersTestList, ...sideMenuTestList, ...materialStandardList, ...classStandardList]\n\n// This function takes a list of tests and a key and returns a list of tests that have the key 'calls'. Then the test is run for each test in the list and the correct button is checked for each scope.\nconst assertCallsList = filterForKey(testList, 'calls')\n\ndescribe.each(assertCallsList)('$title scope test calls', ({ list }) => {\n test.each(list)('$name', async ({ component, scope, route, mockedRoute, calls }) => {\n const initState = { ...actor, scopes: scope }\n render(component, { route: [...route], store: { me: initState } })\n await waitFor(() => {\n expect(mockedRoute).toHaveBeenCalledTimes(calls)\n })\n })\n})\n\n// This function takes a list of tests and a key and returns a list of tests that have the key 'inDocument'. Then the test is run for each test in the list and the correct button is checked for each scope.\nconst assertInDoc = filterForKey(testList, 'inDocument')\ndescribe.each(assertInDoc)('$title scope test assert', ({ list }) => {\n afterEach(() => {\n jest.clearAllMocks()\n })\n test.each(list)('$name', async ({ component, scope, route, inDocument }) => {\n const initState = { ...actor, scopes: scope }\n render(component, { route: [...route], store: { me: initState } })\n await waitFor(() => {\n expect(inDocument(screen)).toBeInTheDocument()\n })\n })\n})\n\n// This function takes a list of tests and a key and returns a list of tests that have the key 'notInDocument'. Then the test is run for each test in the list and the correct button is checked for each scope.\nconst assertNotInDoc = filterForKey(testList, 'notInDocument')\ndescribe.each(assertNotInDoc)('$title scope test assert', ({ list }) => {\n afterEach(() => {\n jest.clearAllMocks()\n })\n test.each(list)('$name', async ({ component, scope, route, notInDocument }) => {\n const initState = { ...actor, scopes: scope }\n render(component, { route: [...route], store: { me: initState } })\n await waitFor(() => {\n expect(notInDocument(screen)).not.toBeInTheDocument()\n })\n })\n})\n\n// This function takes a list of tests and a key and returns a list of tests that have the key.\nfunction filterForKey(obj, key) {\n const filteredList = obj.map(({ title, list }) => ({ title, list: [...list.filter(obj => obj.hasOwnProperty(key))] }))\n return filteredList.filter(({ list }) => list.length > 0)\n}\n" | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment