@oslokommune/punkt-react 19.16.0 → 19.16.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oslokommune/punkt-react",
3
- "version": "19.16.0",
3
+ "version": "19.16.1",
4
4
  "description": "React komponentbibliotek til Punkt, designsystem for Oslo kommune",
5
5
  "homepage": "https://punkt.oslo.kommune.no",
6
6
  "author": "Team Designsystem, Oslo kommune",
@@ -102,5 +102,5 @@
102
102
  "url": "https://github.com/oslokommune/punkt/issues"
103
103
  },
104
104
  "license": "MIT",
105
- "gitHead": "dc04d631131be873574d55919198f719cc0e2d09"
105
+ "gitHead": "1993393adcd70a3f8ee48c7645e546dd78f127e6"
106
106
  }
@@ -1,32 +1,78 @@
1
1
  import { act } from '@testing-library/react'
2
+ import type { ReactElement } from 'react'
2
3
  import { hydrateRoot, type Root } from 'react-dom/client'
3
4
  import { renderToString } from 'react-dom/server'
4
5
  import { vi } from 'vitest'
5
6
 
6
7
  import { serverRenderCases } from '../utils/serverRenderCases'
8
+ import { PktHeaderGlobal, PktHeaderService } from './index'
7
9
 
8
- describe('hydration', () => {
9
- test.each(serverRenderCases)('%s hydrates without mismatches', async (_name, element) => {
10
- const container = document.createElement('div')
11
- container.innerHTML = renderToString(element())
12
- document.body.append(container)
10
+ const hydrate = async (element: () => ReactElement, { serverWithoutWindow = false } = {}) => {
11
+ if (serverWithoutWindow) vi.stubGlobal('window', undefined)
12
+ const html = renderToString(element())
13
+ vi.unstubAllGlobals()
13
14
 
14
- const errors: string[] = []
15
- const consoleError = vi.spyOn(console, 'error').mockImplementation((...args: unknown[]) => {
16
- errors.push(args.map(String).join(' '))
17
- })
15
+ const container = document.createElement('div')
16
+ container.innerHTML = html
17
+ document.body.append(container)
18
+
19
+ const errors: string[] = []
20
+ const consoleError = vi.spyOn(console, 'error').mockImplementation((...args: unknown[]) => {
21
+ errors.push(args.map(String).join(' '))
22
+ })
18
23
 
19
- let root: Root | undefined
20
- await act(async () => {
21
- root = hydrateRoot(container, element(), {
22
- onRecoverableError: (error) => errors.push(String(error)),
23
- })
24
+ let root: Root | undefined
25
+ await act(async () => {
26
+ root = hydrateRoot(container, element(), {
27
+ onRecoverableError: (error) => errors.push(String(error)),
24
28
  })
29
+ })
30
+ consoleError.mockRestore()
25
31
 
26
- consoleError.mockRestore()
32
+ const cleanup = () => {
27
33
  act(() => root?.unmount())
28
34
  container.remove()
35
+ }
36
+ return { container, errors, cleanup }
37
+ }
38
+
39
+ describe('hydration', () => {
40
+ test.each(serverRenderCases)('%s hydrates without mismatches', async (_name, element) => {
41
+ const { errors, cleanup } = await hydrate(element)
42
+ cleanup()
29
43
 
30
44
  expect(errors).toEqual([])
31
45
  })
32
46
  })
47
+
48
+ describe('hydration at phone width', () => {
49
+ let innerWidth: number
50
+
51
+ beforeEach(() => {
52
+ innerWidth = window.innerWidth
53
+ Object.defineProperty(window, 'innerWidth', { value: 375, configurable: true })
54
+ })
55
+
56
+ afterEach(() => {
57
+ Object.defineProperty(window, 'innerWidth', { value: innerWidth, configurable: true })
58
+ })
59
+
60
+ test.each([
61
+ [
62
+ 'PktHeaderService',
63
+ () => <PktHeaderService serviceName="Service" user={{ name: 'Test User' }} logOut={() => {}} />,
64
+ 'pkt-header-service--mobile',
65
+ ],
66
+ ['PktHeaderGlobal', () => <PktHeaderGlobal />, 'pkt-header-global--mobile'],
67
+ ] as const)(
68
+ '%s hydrates the server markup and then switches to the mobile layout',
69
+ async (_name, element, mobileClass) => {
70
+ const { container, errors, cleanup } = await hydrate(element, { serverWithoutWindow: true })
71
+ const headerClasses = container.querySelector('header')?.className.split(' ') ?? []
72
+ cleanup()
73
+
74
+ expect(errors).toEqual([])
75
+ expect(headerClasses).toContain(mobileClass)
76
+ },
77
+ )
78
+ })