From 3c4765d7c7e3f3a51199509dde3e180dd9ecea78 Mon Sep 17 00:00:00 2001 From: Marius Tobiassen Bungum Date: Wed, 15 Jan 2025 13:15:32 +0100 Subject: [PATCH 1/2] :white_check_mark: Write new custom samMock.ts for use in amplify apps --- .../check_config_workflow/list.txt | 2 +- config/config_files/test-utils/portalMock.ts | 301 ------------------ config/config_files/test-utils/samMock.ts | 80 +++++ .../test-utils/setupBrowserTests.ts | 4 +- .../config_files/test-utils/setupNodeTests.ts | 4 +- config/config_files/vitest.config.ts | 73 +++-- config/testutils_config_list.txt | 2 +- 7 files changed, 128 insertions(+), 338 deletions(-) delete mode 100644 config/config_files/test-utils/portalMock.ts create mode 100644 config/config_files/test-utils/samMock.ts diff --git a/config/config_files/check_config_workflow/list.txt b/config/config_files/check_config_workflow/list.txt index f53da9fd1..4909077d6 100644 --- a/config/config_files/check_config_workflow/list.txt +++ b/config/config_files/check_config_workflow/list.txt @@ -13,7 +13,7 @@ setupLocalhost.mjs client/src https://raw.githubusercontent.com/equinor/amplify- setupTests.ts client/src/test-utils https://raw.githubusercontent.com/equinor/amplify-component-lib/refs/heads/main/config/config_files/test-utils/setupTests.ts vitest.d.ts client/src/test-utils https://raw.githubusercontent.com/equinor/amplify-component-lib/refs/heads/main/config/config_files/test-utils/vitest.d.ts browserMocks.ts client/src/test-utils https://raw.githubusercontent.com/equinor/amplify-component-lib/refs/heads/main/config/config_files/test-utils/browserMocks.ts -portalMock.ts client/src/test-utils https://raw.githubusercontent.com/equinor/amplify-component-lib/refs/heads/main/config/config_files/test-utils/portalMock.ts +samMock.ts client/src/test-utils https://raw.githubusercontent.com/equinor/amplify-component-lib/refs/heads/main/config/config_files/test-utils/samMock.ts playwright.ts client/src/test-utils https://raw.githubusercontent.com/equinor/amplify-component-lib/refs/heads/main/config/config_files/test-utils/playwright.ts build.yaml .github/workflows https://raw.githubusercontent.com/equinor/amplify-component-lib/refs/heads/main/config/config_files/workflows/build.yaml e2e.yaml .github/workflows https://raw.githubusercontent.com/equinor/amplify-component-lib/refs/heads/main/config/config_files/workflows/e2e.yaml diff --git a/config/config_files/test-utils/portalMock.ts b/config/config_files/test-utils/portalMock.ts deleted file mode 100644 index a578fabdb..000000000 --- a/config/config_files/test-utils/portalMock.ts +++ /dev/null @@ -1,301 +0,0 @@ -/** - * This file is AUTO GENERATED by [msw-auto-mock](https://github.com/zoubingwu/msw-auto-mock) - * Feel free to commit/edit it as you need. - */ -/* eslint-disable */ -/* tslint:disable */ -import { HttpResponse, http } from 'msw'; -import { faker } from '@faker-js/faker'; - -faker.seed(1); - -const baseURL = '*'; -const MAX_ARRAY_LENGTH = 20; - -let i = 0; -const next = () => { - if (i === Number.MAX_SAFE_INTEGER - 1) { - i = 0; - } - return i++; -}; - -export const handlers = [ - http.get(`${baseURL}/api/v1/Token/AmplifyPortal`, () => { - const resultArray = [ - [getGetAmplifyPortalToken200Response(), { status: 200 }], - ]; - - return HttpResponse.json(...resultArray[next() % resultArray.length]); - }), - http.get(`${baseURL}/api/v1/Token/AmplifyPortal/Production`, () => { - const resultArray = [ - [getGetAmplifyPortalProductionToken200Response(), { status: 200 }], - ]; - - return HttpResponse.json(...resultArray[next() % resultArray.length]); - }), - http.get(`${baseURL}/api/v1/AmplifyApplication/userapplications`, () => { - const resultArray = [ - [getUserApplications200Response(), { status: 200 }], - [getUserApplications400Response(), { status: 400 }], - [null, { status: 500 }], - ]; - - return HttpResponse.json(...resultArray[next() % resultArray.length]); - }), - http.get(`${baseURL}/api/v1/FeatureToggle/:applicationName`, () => { - const resultArray = [ - [getGetFeatureToggleFromApplicationName200Response(), { status: 200 }], - [getGetFeatureToggleFromApplicationName400Response(), { status: 400 }], - [getGetFeatureToggleFromApplicationName404Response(), { status: 404 }], - [getGetFeatureToggleFromApplicationName500Response(), { status: 500 }], - ]; - - return HttpResponse.json(...resultArray[next() % resultArray.length]); - }), - http.get(`${baseURL}/api/v1/AmplifyApplication/token`, () => { - const resultArray = [ - [getGetSasImageToken200Response(), { status: 200 }], - [getGetSasImageToken400Response(), { status: 400 }], - [null, { status: 500 }], - ]; - - return HttpResponse.json(...resultArray[next() % resultArray.length]); - }), - http.get(`${baseURL}/api/v1/ReleaseNotes`, () => { - const resultArray = [[null, { status: 200 }]]; - - return HttpResponse.json(...resultArray[next() % resultArray.length]); - }), - http.get(`${baseURL}/api/v1/ReleaseNotes/myreleasenotes`, () => { - const resultArray = [[null, { status: 200 }]]; - - return HttpResponse.json(...resultArray[next() % resultArray.length]); - }), - http.get(`${baseURL}/api/v1/ReleaseNotes/:applicationName/:releaseId`, () => { - const resultArray = [[null, { status: 200 }]]; - - return HttpResponse.json(...resultArray[next() % resultArray.length]); - }), - http.get(`${baseURL}/api/v1/ReleaseNotes/GetContainerSasUri`, () => { - const resultArray = [ - [getGetContainerSasUri200Response(), { status: 200 }], - [getGetContainerSasUri401Response(), { status: 401 }], - ]; - - return HttpResponse.json(...resultArray[next() % resultArray.length]); - }), - http.post(`${baseURL}/api/v1/ServiceNow/incident`, () => { - const resultArray = [[null, { status: 200 }]]; - - return HttpResponse.json(...resultArray[next() % resultArray.length]); - }), - http.post(`${baseURL}/api/v1/Slack/fileUpload`, () => { - const resultArray = [[null, { status: 200 }]]; - - return HttpResponse.json(...resultArray[next() % resultArray.length]); - }), - http.post(`${baseURL}/api/v1/Slack/postmessage`, () => { - const resultArray = [[null, { status: 200 }]]; - - return HttpResponse.json(...resultArray[next() % resultArray.length]); - }), - http.get(`${baseURL}/api/v1/Tutorial/:applicationName`, () => { - const resultArray = [ - [getGetTutorialsForApplication200Response(), { status: 200 }], - ]; - - return HttpResponse.json(...resultArray[next() % resultArray.length]); - }), - http.get(`${baseURL}/api/v1/Tutorial/SASToken`, () => { - const resultArray = [ - [getGetTutorialSasToken200Response(), { status: 200 }], - ]; - - return HttpResponse.json(...resultArray[next() % resultArray.length]); - }), -]; - -export function getUserApplications200Response() { - return [ - ...new Array(faker.number.int({ min: 1, max: MAX_ARRAY_LENGTH })).keys(), - ].map((_) => ({ - id: faker.lorem.slug(1), - name: faker.person.fullName(), - adGroups: [ - ...new Array(faker.number.int({ min: 1, max: MAX_ARRAY_LENGTH })).keys(), - ].map((_) => faker.lorem.slug(1)), - url: faker.internet.url(), - accessRoles: [ - ...new Array(faker.number.int({ min: 1, max: MAX_ARRAY_LENGTH })).keys(), - ].map((_) => ({ - role: faker.lorem.slug(1), - description: faker.lorem.slug(1), - })), - description: faker.lorem.slug(1), - contentTabs: [ - ...new Array(faker.number.int({ min: 1, max: MAX_ARRAY_LENGTH })).keys(), - ].map((_) => ({ - name: faker.person.fullName(), - chapters: [ - ...new Array( - faker.number.int({ min: 1, max: MAX_ARRAY_LENGTH }) - ).keys(), - ].map((_) => ({ - order: faker.number.int({ min: undefined, max: undefined }), - title: faker.lorem.slug(1), - subTitle: faker.lorem.slug(1), - content: faker.lorem.slug(1), - })), - })), - longDescription: faker.lorem.slug(1), - category: faker.lorem.slug(1), - version: faker.lorem.slug(1), - applicationInsightAPI: faker.lorem.slug(1), - apI_Id: faker.lorem.slug(1), - apiurl: faker.internet.url(), - monitored: faker.datatype.boolean(), - productOwners: [ - ...new Array(faker.number.int({ min: 1, max: MAX_ARRAY_LENGTH })).keys(), - ].map((_) => faker.lorem.slug(1)), - })); -} - -export function getUserApplications400Response() { - return { - type: faker.lorem.slug(1), - title: faker.lorem.slug(1), - status: faker.number.int({ min: undefined, max: undefined }), - detail: faker.lorem.slug(1), - instance: faker.lorem.slug(1), - }; -} - -export function getGetFeatureToggleFromApplicationName200Response() { - return { - applicationName: faker.person.fullName(), - features: [ - ...new Array(faker.number.int({ min: 1, max: MAX_ARRAY_LENGTH })).keys(), - ].map((_) => ({ - uuid: faker.lorem.slug(1), - featureKey: faker.lorem.slug(1), - description: faker.lorem.slug(1), - activeUsers: [ - ...new Array( - faker.number.int({ min: 1, max: MAX_ARRAY_LENGTH }) - ).keys(), - ].map((_) => ({ - id: faker.lorem.slug(1), - displayName: faker.person.fullName(), - mail: faker.lorem.slug(1), - userPrincipalName: faker.person.fullName(), - })), - activeEnvironments: [ - ...new Array( - faker.number.int({ min: 1, max: MAX_ARRAY_LENGTH }) - ).keys(), - ].map((_) => faker.lorem.slug(1)), - })), - }; -} - -export function getGetFeatureToggleFromApplicationName400Response() { - return { - developerMessage: faker.lorem.slug(1), - userMessage: faker.lorem.slug(1), - code: faker.lorem.slug(1), - httpStatusCode: faker.number.int({ min: undefined, max: undefined }), - }; -} - -export function getGetFeatureToggleFromApplicationName404Response() { - return { - developerMessage: faker.lorem.slug(1), - userMessage: faker.lorem.slug(1), - code: faker.lorem.slug(1), - httpStatusCode: faker.number.int({ min: undefined, max: undefined }), - }; -} - -export function getGetFeatureToggleFromApplicationName500Response() { - return { - developerMessage: faker.lorem.slug(1), - userMessage: faker.lorem.slug(1), - code: faker.lorem.slug(1), - httpStatusCode: faker.number.int({ min: undefined, max: undefined }), - }; -} - -export function getGetContainerSasUri200Response() { - return faker.lorem.slug(1); -} - -export function getGetContainerSasUri401Response() { - return { - type: faker.lorem.slug(1), - title: faker.lorem.slug(1), - status: faker.number.int({ min: undefined, max: undefined }), - detail: faker.lorem.slug(1), - instance: faker.lorem.slug(1), - }; -} - -export function getGetTutorialsForApplication200Response() { - return [ - ...new Array(faker.number.int({ min: 1, max: MAX_ARRAY_LENGTH })).keys(), - ].map((_) => ({ - id: faker.number.int().toString(), - name: faker.person.fullName(), - shortName: faker.person.fullName(), - path: faker.lorem.slug(1), - application: faker.lorem.slug(1), - steps: [ - ...new Array(faker.number.int({ min: 1, max: MAX_ARRAY_LENGTH })).keys(), - ].map((_) => ({ - id: faker.number.int().toString(), - title: faker.lorem.slug(1), - body: faker.lorem.slug(1), - key: faker.lorem.slug(1), - position: faker.helpers.arrayElement([ - 'TOP_LEFT', - 'TOP_RIGHT', - 'BOTTOM_LEFT', - 'BOTTOM_RIGHT', - 'CENTER', - ]), - imgUrl: faker.internet.url(), - })), - showInProd: faker.datatype.boolean(), - willPopUp: faker.datatype.boolean(), - dynamicPositioning: faker.datatype.boolean(), - })); -} - -export function getGetTutorialSasToken200Response() { - return faker.lorem.slug(1); -} - -export function getGetSasImageToken200Response() { - return { - uri: faker.lorem.slug(1), - }; -} - -export function getGetSasImageToken400Response() { - return { - type: faker.lorem.slug(1), - title: faker.lorem.slug(1), - status: faker.number.int({ min: undefined, max: undefined }), - detail: faker.lorem.slug(1), - instance: faker.lorem.slug(1), - }; -} - -export function getGetAmplifyPortalProductionToken200Response() { - return faker.lorem.slug(1); -} - -export function getGetAmplifyPortalToken200Response() { - return faker.lorem.slug(1); -} diff --git a/config/config_files/test-utils/samMock.ts b/config/config_files/test-utils/samMock.ts new file mode 100644 index 000000000..ca6800d98 --- /dev/null +++ b/config/config_files/test-utils/samMock.ts @@ -0,0 +1,80 @@ +import { FeatureToggleDto, Tutorial } from '@equinor/subsurface-app-management'; +import { faker } from '@faker-js/faker'; + +import { delay, http, HttpResponse } from 'msw'; + +export const handlers = [ + http.get('*/api/v1/Tutorial/SASToken', async () => { + await delay('real'); + return HttpResponse.text(faker.internet.mac()); + }), + http.get('*/api/v1/Tutorial/{applicationName}', async () => { + await delay('real'); + const body: Tutorial[] = []; + return HttpResponse.json(body); + }), + http.get('*/api/v1/FeatureToggle', async () => { + await delay('real'); + const body: FeatureToggleDto = { + applicationName: 'orca', + features: [], + }; + return HttpResponse.json(body); + }), + http.get('*/api/v1/AmplifyApplication/application/*/appRoles', async () => { + await delay('real'); + return HttpResponse.json([]); + }), + http.get('*/api/v1/ImpersonateUser/CanImpersonate', async () => { + return HttpResponse.text('true'); + }), + http.get('*/api/v1/ImpersonateUser/ActiveUser', async () => { + await delay('real'); + return HttpResponse.json(undefined, { status: 204 }); + }), + http.get('*/api/v1/ImpersonateUser/GetImpersonateUserForApp/*', async () => { + await delay('real'); + return HttpResponse.json([]); + }), + http.get('*/api/v1/Token/AmplifyPortal', async () => { + await delay('real'); + return HttpResponse.text(faker.string.nanoid()); + }), + http.get('*/api/v1/Token/AmplifyPortal/*', async () => { + await delay('real'); + return HttpResponse.text(faker.string.nanoid()); + }), + http.get('*/api/v1/Token/SamPortal', async () => { + await delay('real'); + return HttpResponse.text(faker.string.nanoid()); + }), + http.get('*/api/v1/Token/SamPortal/*', async () => { + await delay('real'); + return HttpResponse.text(faker.string.nanoid()); + }), + http.get('*/api/v1/ReleaseNotes', async () => { + await delay('real'); + return HttpResponse.json([]); + }), + http.get('*/api/v1/ReleaseNotes/:applicationName', async () => { + await delay('real'); + return HttpResponse.json([]); + }), + http.get('*/api/v1/ReleaseNotes/GetContainerSasUri', async () => { + await delay('real'); + return HttpResponse.text( + `${faker.internet.url()}?${faker.string.nanoid()}` + ); + }), + http.get('*/api/v1/AmplifyApplication/userapplications', async () => { + await delay('real'); + return HttpResponse.json([]); + }), + http.get( + '*/api/v1/FeatureToggle/:appName/:currentEnvironment/myfeatures', + async () => { + await delay('real'); + return HttpResponse.json([]); + } + ), +]; diff --git a/config/config_files/test-utils/setupBrowserTests.ts b/config/config_files/test-utils/setupBrowserTests.ts index 2e1360997..b008eb115 100644 --- a/config/config_files/test-utils/setupBrowserTests.ts +++ b/config/config_files/test-utils/setupBrowserTests.ts @@ -2,14 +2,14 @@ import { cleanup } from '@testing-library/react'; import { handlers as customHandlers } from './customMock'; import { handlers as autoGeneratedHandlers } from './mock'; -import { handlers as aclHandlers } from './portalMock'; +import { handlers as samHandlers } from './samMock'; import { setupWorker } from 'msw/browser'; import { afterEach, beforeAll, beforeEach } from 'vitest'; const worker = setupWorker( ...customHandlers, - ...aclHandlers, + ...samHandlers, ...autoGeneratedHandlers ); diff --git a/config/config_files/test-utils/setupNodeTests.ts b/config/config_files/test-utils/setupNodeTests.ts index e9762e32f..2eef98058 100644 --- a/config/config_files/test-utils/setupNodeTests.ts +++ b/config/config_files/test-utils/setupNodeTests.ts @@ -3,7 +3,7 @@ import { cleanup, within } from '@testing-library/react'; import { handlers as customHandlers } from './customMock'; import { handlers as autoGeneratedHandlers } from './mock'; -import { handlers as portalHandlers } from './portalMock'; +import { handlers as samHandlers } from './samMock'; import { MatcherResult } from './vitest'; import { setupServer } from 'msw/node'; @@ -14,7 +14,7 @@ import '@testing-library/jest-dom'; const server = setupServer( ...customHandlers, ...autoGeneratedHandlers, - ...portalHandlers + ...samHandlers ); beforeAll(() => { diff --git a/config/config_files/vitest.config.ts b/config/config_files/vitest.config.ts index d11432dbd..5e98e7bca 100644 --- a/config/config_files/vitest.config.ts +++ b/config/config_files/vitest.config.ts @@ -1,33 +1,44 @@ -import viteTsconfigPaths from 'vite-tsconfig-paths'; -import { defineConfig } from 'vitest/config'; +import { defineConfig, mergeConfig } from 'vitest/config'; +import viteConfig from './vite.config'; -export default defineConfig({ - plugins: [viteTsconfigPaths() as any], - test: { - globals: true, - passWithNoTests: true, - testTimeout: 10000, - env: { - VITE_IS_MOCK: 'true', - }, - exclude: ['**/node_modules/**', '**/test-utils/**', '**/e2e/**'], - coverage: { - enabled: false, - provider: 'v8', - cleanOnRerun: false, - reportOnFailure: true, - include: ['src/**/*'], - exclude: ['src/api', 'src/e2e', 'src/test-utils'], - reporter: [ - 'text-summary', - 'html', - ['json-summary', { file: 'coverage.json' }], - ], - thresholds: { - perFile: true, - autoUpdate: true, - 100: true, +export default defineConfig((configEnv) => + mergeConfig( + viteConfig(configEnv), + defineConfig({ + test: { + globals: true, + passWithNoTests: true, + testTimeout: 10000, + env: { + VITE_IS_MOCK: 'true', + }, + exclude: ['**/node_modules/**', '**/test-utils/**', '**/e2e/**'], + coverage: { + enabled: false, + provider: 'v8', + cleanOnRerun: false, + reportOnFailure: true, + include: ['src/**/*'], + exclude: [ + 'src/api', + 'src/e2e', + 'src/test-utils', + 'src/**/index.ts', + 'src/**/*.test.ts', + 'src/**/*.test.tsx', + ], + reporter: [ + 'text-summary', + 'html', + ['json-summary', { file: 'coverage.json' }], + ], + thresholds: { + perFile: true, + autoUpdate: true, + 100: true, + }, + }, }, - }, - }, -}); + }) + ) +); diff --git a/config/testutils_config_list.txt b/config/testutils_config_list.txt index 1ba2f7f57..723c43334 100644 --- a/config/testutils_config_list.txt +++ b/config/testutils_config_list.txt @@ -3,4 +3,4 @@ https://raw.githubusercontent.com/equinor/amplify-component-lib/refs/heads/main/ https://raw.githubusercontent.com/equinor/amplify-component-lib/refs/heads/main/config/config_files/test-utils/browserMocks.ts https://raw.githubusercontent.com/equinor/amplify-component-lib/refs/heads/main/config/config_files/test-utils/utils.ts https://raw.githubusercontent.com/equinor/amplify-component-lib/refs/heads/main/config/config_files/test-utils/playwright.ts -https://raw.githubusercontent.com/equinor/amplify-component-lib/refs/heads/main/config/config_files/test-utils/portalMock.ts \ No newline at end of file +https://raw.githubusercontent.com/equinor/amplify-component-lib/refs/heads/main/config/config_files/test-utils/samMock.ts \ No newline at end of file From 16c589f3982413fa65e7b49afe716d221b6ec3f3 Mon Sep 17 00:00:00 2001 From: Marius Tobiassen Bungum Date: Wed, 15 Jan 2025 14:55:42 +0100 Subject: [PATCH 2/2] :wrench: Make .prettierrc.js export default (ES module) --- config/config_files/.prettierrc.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config_files/.prettierrc.js b/config/config_files/.prettierrc.js index 1ff74d13c..8c839e8f6 100644 --- a/config/config_files/.prettierrc.js +++ b/config/config_files/.prettierrc.js @@ -1,4 +1,4 @@ -module.exports = { +export default { trailingComma: 'es5', tabWidth: 2, useTabs: false,