|
| 1 | +// Mock the auxia module before imports so the mock is applied when the module |
| 2 | +// under test is evaluated. |
| 3 | +import { buildAuxiaGateDisplayData } from '../../lib/auxia'; |
| 4 | +import type { AuxiaGateDisplayData } from '../SignInGate/types'; |
1 | 5 | import { canShowSignInGatePortal } from './SignInGatePortal'; |
2 | 6 |
|
| 7 | +// Mock the auxia module (Jest hoists jest.mock calls so placing it after imports is fine). |
| 8 | +jest.mock('../../lib/auxia', () => ({ |
| 9 | + buildAuxiaGateDisplayData: jest.fn(), |
| 10 | +})); |
| 11 | + |
3 | 12 | // Mock document.getElementById |
4 | 13 | const mockGetElementById = jest.fn(); |
5 | 14 | Object.defineProperty(document, 'getElementById', { |
@@ -52,22 +61,83 @@ describe('SignInGatePortal', () => { |
52 | 61 | const mockElement = document.createElement('div'); |
53 | 62 | mockGetElementById.mockReturnValue(mockElement); |
54 | 63 |
|
55 | | - const result = await canShowSignInGatePortal(false, false, false); |
| 64 | + // Mock buildAuxiaGateDisplayData to return auxiaData with userTreatment |
| 65 | + const auxiaReturn: AuxiaGateDisplayData = { |
| 66 | + browserId: 'browser-1', |
| 67 | + auxiaData: { |
| 68 | + responseId: 'resp1', |
| 69 | + userTreatment: { |
| 70 | + treatmentId: 't1', |
| 71 | + treatmentTrackingId: 'tt1', |
| 72 | + rank: '1', |
| 73 | + contentLanguageCode: 'en', |
| 74 | + treatmentContent: 'content', |
| 75 | + treatmentType: 'type', |
| 76 | + surface: 'surface', |
| 77 | + }, |
| 78 | + }, |
| 79 | + }; |
| 80 | + ( |
| 81 | + buildAuxiaGateDisplayData as jest.MockedFunction< |
| 82 | + typeof buildAuxiaGateDisplayData |
| 83 | + > |
| 84 | + ).mockResolvedValue(auxiaReturn); |
| 85 | + |
| 86 | + const result = await canShowSignInGatePortal( |
| 87 | + false, // isSignedIn |
| 88 | + false, // isPaidContent |
| 89 | + false, // isPreview |
| 90 | + 'page-id', // pageId |
| 91 | + 'https://contributions.local', // contributionsServiceUrl |
| 92 | + 'UK', // editionId |
| 93 | + 'Article', // contentType (must be a valid content type) |
| 94 | + 'section', // sectionId |
| 95 | + [], // tags |
| 96 | + () => 0, // retrieveDismissedCount |
| 97 | + ); |
56 | 98 |
|
57 | | - expect(result).toEqual({ show: true, meta: undefined }); |
| 99 | + expect(result).toEqual({ show: true, meta: auxiaReturn }); |
58 | 100 | }); |
59 | 101 |
|
60 | | - it('should return false when isSignedIn is undefined but truthy check would pass', async () => { |
| 102 | + it('should return true when isSignedIn is undefined but other params allow gate', async () => { |
61 | 103 | const mockElement = document.createElement('div'); |
62 | 104 | mockGetElementById.mockReturnValue(mockElement); |
63 | 105 |
|
| 106 | + const auxiaReturn: AuxiaGateDisplayData = { |
| 107 | + browserId: 'browser-2', |
| 108 | + auxiaData: { |
| 109 | + responseId: 'resp2', |
| 110 | + userTreatment: { |
| 111 | + treatmentId: 't2', |
| 112 | + treatmentTrackingId: 'tt2', |
| 113 | + rank: '1', |
| 114 | + contentLanguageCode: 'en', |
| 115 | + treatmentContent: 'content', |
| 116 | + treatmentType: 'type', |
| 117 | + surface: 'surface', |
| 118 | + }, |
| 119 | + }, |
| 120 | + }; |
| 121 | + ( |
| 122 | + buildAuxiaGateDisplayData as jest.MockedFunction< |
| 123 | + typeof buildAuxiaGateDisplayData |
| 124 | + > |
| 125 | + ).mockResolvedValue(auxiaReturn); |
| 126 | + |
64 | 127 | const result = await canShowSignInGatePortal( |
65 | 128 | undefined, |
66 | 129 | false, |
67 | 130 | false, |
| 131 | + 'page-id', |
| 132 | + 'https://contributions.local', |
| 133 | + 'UK', |
| 134 | + 'Article', |
| 135 | + 'section', |
| 136 | + [], |
| 137 | + () => 0, |
68 | 138 | ); |
69 | 139 |
|
70 | | - expect(result).toEqual({ show: true, meta: undefined }); |
| 140 | + expect(result).toEqual({ show: true, meta: auxiaReturn }); |
71 | 141 | }); |
72 | 142 | }); |
73 | 143 | }); |
0 commit comments