-
Notifications
You must be signed in to change notification settings - Fork 114
test: increase code coverage pt 2 #1411
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 all commits
19dcc73
708fe48
35ae9e9
19b983a
8b72421
9d30e59
c338604
0c34440
264a025
ef50e18
c18a8b7
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 |
|---|---|---|
| @@ -1,9 +1,14 @@ | ||
| import { TestEnvironment } from '__test__/support/environment/TestEnvironment'; | ||
| import { setupLoadStylesheet } from '__test__/support/helpers/setup'; | ||
| import { DelayedPromptType } from 'src/shared/prompts/constants'; | ||
| import type { | ||
| AppUserConfigPromptOptions, | ||
| DelayedPromptOptions, | ||
| DelayedPromptTypeValue, | ||
| } from 'src/shared/prompts/types'; | ||
| import { Browser } from 'src/shared/useragent/constants'; | ||
| import * as detect from 'src/shared/useragent/detect'; | ||
| import { PromptsManager } from './PromptsManager'; | ||
|
|
||
| const getBrowserNameSpy = vi.spyOn(detect, 'getBrowserName'); | ||
| const getBrowserVersionSpy = vi.spyOn(detect, 'getBrowserVersion'); | ||
| const isMobileBrowserSpy = vi.spyOn(detect, 'isMobileBrowser'); | ||
|
|
@@ -52,4 +57,62 @@ describe('PromptsManager', () => { | |
| await pm['_internalShowSlidedownPrompt'](); | ||
| expect(installSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| test('_internalShowDelayedPrompt forces slidedown when interaction required', async () => { | ||
| requiresUserInteractionSpy.mockReturnValue(true); | ||
| const pm = new PromptsManager(OneSignal._context); | ||
| const nativeSpy = vi | ||
| .spyOn(pm, '_internalShowNativePrompt') | ||
| .mockResolvedValue(true); | ||
| const slidedownSpy = vi | ||
|
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. could just make _internalShowSlidedownPrompt public since it gets mangled anyway |
||
| .spyOn( | ||
| PromptsManager.prototype, | ||
| '_internalShowSlidedownPrompt' as keyof PromptsManager, | ||
| ) | ||
| .mockResolvedValue(undefined as void); | ||
| await pm._internalShowDelayedPrompt(DelayedPromptType._Native, 0); | ||
| expect(nativeSpy).not.toHaveBeenCalled(); | ||
| expect(slidedownSpy).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('_spawnAutoPrompts triggers native when condition met and not forced', async () => { | ||
| const pm = new PromptsManager(OneSignal._context); | ||
| const getOptsSpy = vi | ||
| .spyOn( | ||
| pm as unknown as { | ||
|
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. same thing here |
||
| _getDelayedPromptOptions: ( | ||
| opts: AppUserConfigPromptOptions | undefined, | ||
| type: DelayedPromptTypeValue, | ||
| ) => DelayedPromptOptions; | ||
| }, | ||
| '_getDelayedPromptOptions', | ||
| ) | ||
| .mockImplementation( | ||
| (): DelayedPromptOptions => ({ | ||
| enabled: true, | ||
| autoPrompt: true, | ||
| timeDelay: 0, | ||
| pageViews: 0, | ||
| }), | ||
| ); | ||
| const condSpy = vi | ||
|
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. _isPageViewConditionMet can be separated out of this class since theres no this usage,
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. Then it test can do: |
||
| .spyOn( | ||
| pm as unknown as { _isPageViewConditionMet: (o?: unknown) => boolean }, | ||
| '_isPageViewConditionMet', | ||
| ) | ||
| .mockReturnValue(true); | ||
| const delayedSpy = vi | ||
| .spyOn( | ||
| PromptsManager.prototype, | ||
| '_internalShowDelayedPrompt' as keyof PromptsManager, | ||
| ) | ||
| .mockResolvedValue(undefined as void); | ||
| requiresUserInteractionSpy.mockReturnValue(false); | ||
| getBrowserNameSpy.mockReturnValue(Browser._Chrome); | ||
| getBrowserVersionSpy.mockReturnValue(62); | ||
| await pm._spawnAutoPrompts(); | ||
| expect(getOptsSpy).toHaveBeenCalled(); | ||
| expect(condSpy).toHaveBeenCalled(); | ||
| expect(delayedSpy).toHaveBeenCalledWith(DelayedPromptType._Native, 0); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { TestEnvironment } from '__test__/support/environment/TestEnvironment'; | ||
| import { setupLoadStylesheet } from '__test__/support/helpers/setup'; | ||
| import { | ||
| CUSTOM_LINK_CSS_CLASSES, | ||
| CUSTOM_LINK_CSS_SELECTORS, | ||
| } from 'src/shared/slidedown/constants'; | ||
| import { vi, type MockInstance } from 'vitest'; | ||
| import { ResourceLoadState } from '../../page/services/DynamicResourceLoader'; | ||
| import { CustomLinkManager } from './CustomLinkManager'; | ||
|
|
||
| describe('CustomLinkManager', () => { | ||
| let isPushEnabledSpy: MockInstance; | ||
| beforeEach(() => { | ||
| TestEnvironment.initialize(); | ||
| document.body.innerHTML = ` | ||
| <div class="${CUSTOM_LINK_CSS_SELECTORS._ContainerSelector.replace('.', '')}"></div> | ||
| `; | ||
| isPushEnabledSpy = vi.spyOn( | ||
| OneSignal._context._subscriptionManager, | ||
| '_isPushNotificationsEnabled', | ||
| ); | ||
| }); | ||
|
|
||
| test('_initialize returns when disabled or stylesheet fails', async () => { | ||
| const mgrDisabled = new CustomLinkManager({ enabled: false }); | ||
| await expect(mgrDisabled._initialize()).resolves.toBeUndefined(); | ||
|
|
||
| // Stylesheet not loaded | ||
| const mgr = new CustomLinkManager({ | ||
| enabled: true, | ||
| text: { explanation: 'x', subscribe: 'Sub' }, | ||
| }); | ||
| vi.spyOn( | ||
| OneSignal._context._dynamicResourceLoader, | ||
| '_loadSdkStylesheet', | ||
| ).mockResolvedValue(ResourceLoadState._Failed); | ||
| await mgr._initialize(); | ||
|
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. nit: new line after |
||
| // nothing injected | ||
| const containers = document.querySelectorAll( | ||
| CUSTOM_LINK_CSS_SELECTORS._ContainerSelector, | ||
| ); | ||
| expect(containers.length).toBe(1); | ||
| expect(containers[0].children.length).toBe(0); | ||
| }); | ||
|
|
||
| test('_initialize hides containers when subscribed and unsubscribe disabled', async () => { | ||
| await setupLoadStylesheet(); | ||
| isPushEnabledSpy.mockResolvedValue(true); | ||
| const mgr = new CustomLinkManager({ | ||
| enabled: true, | ||
| unsubscribeEnabled: false, | ||
| text: { | ||
| explanation: 'hello', | ||
| subscribe: 'Subscribe', | ||
| unsubscribe: 'Unsubscribe', | ||
| }, | ||
| }); | ||
| await mgr._initialize(); | ||
| const containers = document.querySelectorAll<HTMLElement>( | ||
| CUSTOM_LINK_CSS_SELECTORS._ContainerSelector, | ||
| ); | ||
| expect( | ||
| containers[0].classList.contains(CUSTOM_LINK_CSS_CLASSES._Hide), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| test('_initialize injects markup and click toggles subscription', async () => { | ||
| await setupLoadStylesheet(); | ||
| isPushEnabledSpy.mockResolvedValue(false); | ||
| const optInSpy = vi | ||
| .spyOn(OneSignal.User.PushSubscription, 'optIn') | ||
| .mockResolvedValue(); | ||
| const optOutSpy = vi | ||
| .spyOn(OneSignal.User.PushSubscription, 'optOut') | ||
| .mockResolvedValue(); | ||
| const mgr = new CustomLinkManager({ | ||
| enabled: true, | ||
| unsubscribeEnabled: true, | ||
| text: { | ||
| explanation: 'hello', | ||
| subscribe: 'Subscribe', | ||
| unsubscribe: 'Unsubscribe', | ||
| }, | ||
| style: 'button', | ||
| size: 'medium', | ||
| color: { text: '#fff', button: '#000' }, | ||
| }); | ||
| await mgr._initialize(); | ||
| const button = document.querySelector<HTMLButtonElement>( | ||
| `.${CUSTOM_LINK_CSS_CLASSES._SubscribeClass}`, | ||
| ); | ||
| expect(button).not.toBeNull(); | ||
| expect(button?.textContent).toBe('Subscribe'); | ||
|
|
||
| await button?.click(); | ||
| expect(optInSpy).toHaveBeenCalled(); | ||
|
|
||
| // simulate subscribed now (set optedIn getter) | ||
| vi.spyOn(OneSignal.User.PushSubscription, 'optedIn', 'get').mockReturnValue( | ||
| true, | ||
| ); | ||
| await button?.click(); | ||
| expect(optOutSpy).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
nit: add new lines after expects