-
-
Notifications
You must be signed in to change notification settings - Fork 0
🧪 test(frontend): comprehensive tests for network-consent #19
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
Open
Adityavanjre
wants to merge
1
commit into
main
Choose a base branch
from
fix-network-consent-tests-7997950237695755626
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| module.exports = { | ||
| preset: 'ts-jest', | ||
| testEnvironment: 'jsdom', | ||
| moduleNameMapper: { | ||
| '^@/(.*)$': '<rootDir>/src/$1', | ||
| }, | ||
| setupFilesAfterEnv: ['<rootDir>/jest.setup.js'], | ||
| }; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| require('@testing-library/jest-dom'); |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| /** | ||
| * @jest-environment node | ||
| */ | ||
| describe('network-consent (node)', () => { | ||
| it('should return true when window is undefined', async () => { | ||
| jest.resetModules(); | ||
| const networkConsentModule = await import('./network-consent'); | ||
| expect(networkConsentModule.hasNetworkConsent()).toBe(true); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| describe('network-consent', () => { | ||
| let hasNetworkConsent: typeof import('./network-consent').hasNetworkConsent; | ||
|
|
||
| beforeEach(async () => { | ||
| jest.resetModules(); | ||
| const networkConsentModule = await import('./network-consent'); | ||
| hasNetworkConsent = networkConsentModule.hasNetworkConsent; | ||
| // Clear out any mocked window.sessionStorage between tests | ||
| // @ts-expect-error Type deletion is fine in tests to clean mock overrides | ||
| delete window.sessionStorage; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| describe('hasNetworkConsent', () => { | ||
| it('should return false if sessionStorage is empty and cache is not set', async () => { | ||
| const mockGetItem = jest.fn().mockReturnValue(null); | ||
| Object.defineProperty(window, 'sessionStorage', { | ||
| value: { getItem: mockGetItem }, | ||
| writable: true, | ||
| configurable: true, | ||
| }); | ||
|
|
||
| expect(hasNetworkConsent()).toBe(false); | ||
| expect(mockGetItem).toHaveBeenCalledWith('k_network_consent_granted'); | ||
| }); | ||
|
|
||
| it('should return true if sessionStorage has granted', async () => { | ||
| const mockGetItem = jest.fn().mockReturnValue('granted'); | ||
| Object.defineProperty(window, 'sessionStorage', { | ||
| value: { getItem: mockGetItem }, | ||
| writable: true, | ||
| configurable: true, | ||
| }); | ||
|
|
||
| expect(hasNetworkConsent()).toBe(true); | ||
| expect(mockGetItem).toHaveBeenCalledWith('k_network_consent_granted'); | ||
| }); | ||
|
|
||
| it('should return false if sessionStorage throws an error', async () => { | ||
| Object.defineProperty(window, 'sessionStorage', { | ||
| get: () => { | ||
| throw new Error('Access denied'); | ||
| }, | ||
| configurable: true, | ||
| }); | ||
|
|
||
| expect(hasNetworkConsent()).toBe(false); | ||
| }); | ||
|
|
||
| it('should use consentCache if already set', async () => { | ||
| // First call to set the cache | ||
| const mockGetItem = jest.fn().mockReturnValue('granted'); | ||
| Object.defineProperty(window, 'sessionStorage', { | ||
| value: { getItem: mockGetItem }, | ||
| writable: true, | ||
| configurable: true, | ||
| }); | ||
|
|
||
| expect(hasNetworkConsent()).toBe(true); | ||
| expect(mockGetItem).toHaveBeenCalledTimes(1); | ||
|
|
||
| // Change session storage, shouldn't be called because cache is true | ||
| mockGetItem.mockReturnValue(null); | ||
| expect(hasNetworkConsent()).toBe(true); | ||
| expect(mockGetItem).toHaveBeenCalledTimes(1); // Still 1 | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Because
/workspace/ERP/package.jsondeclaresnexus/frontendas a workspace, root-level installs use/workspace/ERP/package-lock.json, not the newly addednexus/package-lock.json. That existing root lock still has no entries for the newly added frontend test deps such as@testing-library/jest-dom,@testing-library/react, orjest-environment-jsdom, andnpm ciis documented to error whenpackage.jsonand the package lock disagree; CI/dev flows that runnpm ci --workspace nexus/frontendfrom the repo root will not get a reproducible install for the newnpm testscript. Please update the root lockfile alongside these deps.Useful? React with 👍 / 👎.