-
Notifications
You must be signed in to change notification settings - Fork 1
Add devMode for NAC widgets #969
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 7 commits
0e3da7c
5f354e3
6852c7c
f96d272
083c457
3e5fc08
514ccdc
840909d
251372b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| const mockCenters: Record< | ||
| string, | ||
| { warnings: boolean; forecasts: boolean; stations: boolean; obs: boolean; weather: boolean } | ||
| > = { | ||
| nwac: { | ||
| warnings: true, | ||
| forecasts: true, | ||
| stations: true, | ||
| obs: true, | ||
| weather: true, | ||
| }, | ||
| sac: { | ||
| warnings: true, | ||
| forecasts: true, | ||
| stations: false, | ||
| obs: true, | ||
| weather: false, | ||
| }, | ||
| } | ||
|
|
||
| const allFalsePlatforms = { | ||
| warnings: false, | ||
| forecasts: false, | ||
| stations: false, | ||
| obs: false, | ||
| weather: false, | ||
| } | ||
|
|
||
| // Mock the entire nac module, re-implementing getAvalancheCenterPlatforms | ||
| // with the real logic but using mocked data instead of fetching from the API | ||
| jest.mock('../../src/services/nac/nac', () => ({ | ||
| getAvalancheCenterPlatforms: jest.fn(async (centerSlug: string) => { | ||
| const centerSlugToUse = centerSlug === 'dvac' ? 'nwac' : centerSlug | ||
|
|
||
| return mockCenters[centerSlugToUse] ?? allFalsePlatforms | ||
| }), | ||
| })) | ||
|
|
||
| import { getAvalancheCenterPlatforms } from '@/services/nac/nac' | ||
|
|
||
| describe('services: getAvalancheCenterPlatforms', () => { | ||
| it('returns platforms for a matching center slug', async () => { | ||
| const result = await getAvalancheCenterPlatforms('nwac') | ||
| expect(result).toEqual({ | ||
| warnings: true, | ||
| forecasts: true, | ||
| stations: true, | ||
| obs: true, | ||
| weather: true, | ||
| }) | ||
| }) | ||
|
|
||
| it('maps dvac slug to nwac', async () => { | ||
| const result = await getAvalancheCenterPlatforms('dvac') | ||
| expect(result).toEqual({ | ||
| warnings: true, | ||
| forecasts: true, | ||
| stations: true, | ||
| obs: true, | ||
| weather: true, | ||
| }) | ||
| }) | ||
|
|
||
| it('uppercases the slug for matching', async () => { | ||
| const result = await getAvalancheCenterPlatforms('sac') | ||
| expect(result).toEqual({ | ||
| warnings: true, | ||
| forecasts: true, | ||
| stations: false, | ||
| obs: true, | ||
| weather: false, | ||
| }) | ||
| }) | ||
|
|
||
| it('returns all-false platforms when center is not found', async () => { | ||
| const result = await getAvalancheCenterPlatforms('unknown') | ||
| expect(result).toEqual(allFalsePlatforms) | ||
| }) | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { getNACWidgetsConfig } from '@/utilities/getNACWidgetsConfig' | ||
|
|
||
| const mockConfig: { version: string; baseUrl: string; devMode?: boolean } = { | ||
| version: '20251207', | ||
| baseUrl: 'https://du6amfiq9m9h7.cloudfront.net/public/v2', | ||
| devMode: false, | ||
| } | ||
|
|
||
| jest.mock('../../src/utilities/getGlobals', () => ({ | ||
| getCachedGlobal: () => () => Promise.resolve(mockConfig), | ||
| })) | ||
|
|
||
| describe('utilities: getNACWidgetsConfig', () => { | ||
| const originalNodeEnv = process.env.NODE_ENV | ||
|
|
||
| afterEach(() => { | ||
| Object.defineProperty(process.env, 'NODE_ENV', { | ||
| value: originalNodeEnv, | ||
| configurable: true, | ||
| }) | ||
| mockConfig.devMode = false | ||
| }) | ||
|
|
||
| it('returns version and baseUrl from the global config', async () => { | ||
| const result = await getNACWidgetsConfig() | ||
| expect(result.version).toBe('20251207') | ||
| expect(result.baseUrl).toBe('https://du6amfiq9m9h7.cloudfront.net/public/v2') | ||
| }) | ||
|
|
||
| it('forces devMode to false in production even when global config has it enabled', async () => { | ||
| Object.defineProperty(process.env, 'NODE_ENV', { value: 'production', configurable: true }) | ||
| mockConfig.devMode = true | ||
|
|
||
| const result = await getNACWidgetsConfig() | ||
| expect(result.devMode).toBe(false) | ||
| }) | ||
|
Comment on lines
+44
to
+51
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The PR description says that
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah good shout - this was implemented incorrectly and overengineered. I somehow got confused. The priority is:
|
||
|
|
||
| it('defaults devMode to false when not set in global config', async () => { | ||
| Object.defineProperty(process.env, 'NODE_ENV', { value: 'development', configurable: true }) | ||
| mockConfig.devMode = undefined | ||
|
|
||
| const result = await getNACWidgetsConfig() | ||
| expect(result.devMode).toBe(false) | ||
| }) | ||
|
|
||
| it('throws when version is missing', async () => { | ||
| const original = mockConfig.version | ||
| mockConfig.version = '' | ||
|
|
||
| await expect(getNACWidgetsConfig()).rejects.toThrow('Could not determine NAC widgets version') | ||
|
|
||
| mockConfig.version = original | ||
| }) | ||
|
|
||
| it('throws when baseUrl is missing', async () => { | ||
| const original = mockConfig.baseUrl | ||
| mockConfig.baseUrl = '' | ||
|
|
||
| await expect(getNACWidgetsConfig()).rejects.toThrow('Could not determine NAC widgets base url') | ||
|
|
||
| mockConfig.baseUrl = original | ||
| }) | ||
| }) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replacing the function we're testing here with a mocked function that's only used in this test doesn't actually test the getAvalancheCenterPlatforms function.
Ideally, we would intercept the API requests and return mocked data using MSW or something similar so that we would be testing the actual implementation of
getAvalancheCenterPlatforms.If you don't want to set up MSW, I suggest removing this test file completely.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good callout - set up MSW you linked and implemented this in 251372b