Skip to content

Commit 0744594

Browse files
committed
Fix Social/Shares unit test case - navigator not properly mocked in JSDOM environment
1 parent 278c616 commit 0744594

1 file changed

Lines changed: 48 additions & 6 deletions

File tree

src/components/Social/Shares/client/__tests__/index.spec.ts

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,52 @@ describe('SocialShareElement web component', () => {
5252
throw new Error('window context is required for SocialShareElement tests')
5353
}
5454

55-
;(globalThis as typeof globalThis & { MouseEvent?: typeof window.MouseEvent }).MouseEvent =
56-
window.MouseEvent
57-
;(globalThis as typeof globalThis & { navigator?: Navigator }).navigator = window.navigator
58-
59-
await assertion({ element, window })
55+
const restoreGlobals = installBrowserGlobals(window)
56+
try {
57+
await assertion({ element, window })
58+
} finally {
59+
restoreGlobals()
60+
}
6061
},
6162
})
6263
}
6364

65+
type BrowserGlobalKey = 'MouseEvent' | 'navigator'
66+
67+
const installBrowserGlobals = (window: Window & typeof globalThis) => {
68+
const previousDescriptors: Partial<Record<BrowserGlobalKey, PropertyDescriptor | undefined>> = {
69+
MouseEvent: Object.getOwnPropertyDescriptor(globalThis, 'MouseEvent'),
70+
navigator: Object.getOwnPropertyDescriptor(globalThis, 'navigator'),
71+
}
72+
73+
if (window.MouseEvent) {
74+
Object.defineProperty(globalThis, 'MouseEvent', {
75+
configurable: true,
76+
value: window.MouseEvent,
77+
})
78+
}
79+
80+
Object.defineProperty(globalThis, 'navigator', {
81+
configurable: true,
82+
value: window.navigator,
83+
})
84+
85+
return () => restoreBrowserGlobals(previousDescriptors)
86+
}
87+
88+
const restoreBrowserGlobals = (
89+
descriptors: Partial<Record<BrowserGlobalKey, PropertyDescriptor | undefined>>,
90+
) => {
91+
for (const key of Object.keys(descriptors) as BrowserGlobalKey[]) {
92+
const descriptor = descriptors[key]
93+
if (descriptor) {
94+
Object.defineProperty(globalThis, key, descriptor)
95+
} else {
96+
delete (globalThis as Record<string, unknown>)[key]
97+
}
98+
}
99+
}
100+
64101
test('renders buttons for the requested social networks', async () => {
65102
await renderComponent(
66103
{
@@ -117,6 +154,11 @@ describe('SocialShareElement web component', () => {
117154
const shareSpy = vi.fn().mockResolvedValue(undefined)
118155
;(window.navigator as Navigator & { share?: typeof shareSpy }).share = shareSpy
119156

157+
window.document.title = defaultProps.title
158+
const shareUrl = new URL(defaultProps.url)
159+
window.history.replaceState({}, '', shareUrl.pathname)
160+
const expectedLocation = window.location.href
161+
120162
const metaDescription = window.document.createElement('meta')
121163
metaDescription.name = 'description'
122164
metaDescription.content = 'Meta description copy'
@@ -135,7 +177,7 @@ describe('SocialShareElement web component', () => {
135177
expect(shareSpy).toHaveBeenCalledWith({
136178
title: defaultProps.title,
137179
text: 'Meta description copy',
138-
url: defaultProps.url,
180+
url: expectedLocation,
139181
})
140182
})
141183
})

0 commit comments

Comments
 (0)