@jpmorganchase/mosaic-store 0.1.0-beta.8
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/.turbo/turbo-build.log +4 -0
- package/CHANGELOG.md +7 -0
- package/README.md +9 -0
- package/dist/chunk-5L7EI47O.js +0 -0
- package/dist/chunk-5TAZCOCH.js +1 -0
- package/dist/chunk-AA7IK7Y5.js +1 -0
- package/dist/chunk-ASPK4M5R.js +0 -0
- package/dist/chunk-BHMVW2ZZ.js +0 -0
- package/dist/chunk-BZYGOU7Y.js +0 -0
- package/dist/chunk-C62PIKZ5.js +1 -0
- package/dist/chunk-DGGPN6YK.js +1 -0
- package/dist/chunk-HUM6DWGV.js +1 -0
- package/dist/chunk-LYZGAQ5N.js +1 -0
- package/dist/chunk-MBLLQB7A.js +0 -0
- package/dist/chunk-OHN6TEOW.js +1 -0
- package/dist/chunk-PN7DOU52.js +1 -0
- package/dist/chunk-SD2G45FB.js +1 -0
- package/dist/chunk-TEDOG7PC.js +1 -0
- package/dist/chunk-UGD4MMRB.js +0 -0
- package/dist/chunk-UK5NIPTI.js +0 -0
- package/dist/chunk-UMYWSM5J.js +1 -0
- package/dist/chunk-VXYECVIB.js +0 -0
- package/dist/chunk-ZJYDN5YH.js +0 -0
- package/dist/chunk-ZYJGHEAC.js +0 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +1 -0
- package/dist/store.d.ts +30 -0
- package/dist/store.js +1 -0
- package/dist/types/appHeader.d.ts +66 -0
- package/dist/types/appHeader.js +1 -0
- package/dist/types/breadcrumbs.d.ts +20 -0
- package/dist/types/breadcrumbs.js +1 -0
- package/dist/types/footer.d.ts +16 -0
- package/dist/types/footer.js +1 -0
- package/dist/types/helpLinks.d.ts +13 -0
- package/dist/types/helpLinks.js +1 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/index.js +1 -0
- package/dist/types/layout.d.ts +7 -0
- package/dist/types/layout.js +1 -0
- package/dist/types/meta.d.ts +12 -0
- package/dist/types/meta.js +1 -0
- package/dist/types/route.d.ts +7 -0
- package/dist/types/route.js +1 -0
- package/dist/types/sharedConfig.d.ts +14 -0
- package/dist/types/sharedConfig.js +1 -0
- package/dist/types/sidebar.d.ts +22 -0
- package/dist/types/sidebar.js +1 -0
- package/dist/types/tableOfContents.d.ts +18 -0
- package/dist/types/tableOfContents.js +1 -0
- package/dist/useAppHeader.d.ts +9 -0
- package/dist/useAppHeader.js +1 -0
- package/dist/useBreadcrumbs.d.ts +5 -0
- package/dist/useBreadcrumbs.js +1 -0
- package/dist/useFooter.d.ts +4 -0
- package/dist/useFooter.js +1 -0
- package/dist/useLayout.d.ts +4 -0
- package/dist/useLayout.js +1 -0
- package/dist/useMeta.d.ts +5 -0
- package/dist/useMeta.js +1 -0
- package/dist/useNextPrev.d.ts +4 -0
- package/dist/useNextPrev.js +1 -0
- package/dist/useRoute.d.ts +2 -0
- package/dist/useRoute.js +1 -0
- package/dist/useSidebar.d.ts +5 -0
- package/dist/useSidebar.js +1 -0
- package/dist/useTableOfContents.d.ts +4 -0
- package/dist/useTableOfContents.js +1 -0
- package/package.json +32 -0
- package/src/__tests__/store.test.ts +126 -0
- package/src/__tests__/test-utils/utils.tsx +25 -0
- package/src/__tests__/useAppHeader.test.ts +182 -0
- package/src/__tests__/useBreadcrumbs.test.ts +42 -0
- package/src/__tests__/useFooter.test.ts +64 -0
- package/src/__tests__/useLayout.test.ts +24 -0
- package/src/__tests__/useMeta.test.ts +35 -0
- package/src/__tests__/useRoute.test.ts +24 -0
- package/src/__tests__/useTableOfContents.test.ts +31 -0
- package/src/index.ts +13 -0
- package/src/store.ts +105 -0
- package/src/types/appHeader.ts +72 -0
- package/src/types/breadcrumbs.ts +21 -0
- package/src/types/footer.ts +17 -0
- package/src/types/helpLinks.ts +13 -0
- package/src/types/index.ts +10 -0
- package/src/types/layout.ts +7 -0
- package/src/types/meta.ts +13 -0
- package/src/types/route.ts +7 -0
- package/src/types/sharedConfig.ts +16 -0
- package/src/types/sidebar.ts +22 -0
- package/src/types/tableOfContents.ts +19 -0
- package/src/useAppHeader.ts +64 -0
- package/src/useBreadcrumbs.ts +11 -0
- package/src/useFooter.ts +10 -0
- package/src/useLayout.ts +9 -0
- package/src/useMeta.ts +17 -0
- package/src/useNextPrev.ts +43 -0
- package/src/useRoute.ts +9 -0
- package/src/useSidebar.ts +16 -0
- package/src/useTableOfContents.ts +10 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { renderHook } from '@testing-library/react-hooks';
|
|
2
|
+
|
|
3
|
+
import { createWrapper } from './test-utils/utils';
|
|
4
|
+
import { initializeStore, useCreateStore, useStore } from '../store';
|
|
5
|
+
|
|
6
|
+
describe('GIVEN the store initializer', () => {
|
|
7
|
+
describe('WHEN no other state is provided', () => {
|
|
8
|
+
test('THEN the store api provides the initial default state', () => {
|
|
9
|
+
const storeApi = initializeStore();
|
|
10
|
+
expect(storeApi.getState()).toEqual({
|
|
11
|
+
breadcrumbs: [],
|
|
12
|
+
sidebarData: [],
|
|
13
|
+
tableOfContents: [],
|
|
14
|
+
sharedConfig: {},
|
|
15
|
+
description: undefined,
|
|
16
|
+
layout: undefined,
|
|
17
|
+
route: undefined,
|
|
18
|
+
title: undefined
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe('WHEN additional state is preloaded', () => {
|
|
24
|
+
test('THEN the store api provides the initial default state plus the preloaded state', () => {
|
|
25
|
+
const state = {
|
|
26
|
+
description: 'description',
|
|
27
|
+
layout: 'layout',
|
|
28
|
+
route: 'route',
|
|
29
|
+
title: 'title'
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const storeApi = initializeStore(state);
|
|
33
|
+
expect(storeApi.getState()).toEqual({
|
|
34
|
+
breadcrumbs: [],
|
|
35
|
+
sidebarData: [],
|
|
36
|
+
tableOfContents: [],
|
|
37
|
+
sharedConfig: {},
|
|
38
|
+
...state
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe('GIVEN the `useCreateStore` hook', () => {
|
|
45
|
+
describe('WHEN executed on the client', () => {
|
|
46
|
+
test('THEN the store content is replaced', () => {
|
|
47
|
+
const state = { layout: 'layout', description: 'des' };
|
|
48
|
+
const { result, rerender } = renderHook(() => useCreateStore(state));
|
|
49
|
+
expect(result.current().getState()).toEqual({
|
|
50
|
+
breadcrumbs: [],
|
|
51
|
+
sidebarData: [],
|
|
52
|
+
tableOfContents: [],
|
|
53
|
+
sharedConfig: {},
|
|
54
|
+
...state,
|
|
55
|
+
route: undefined,
|
|
56
|
+
title: undefined
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// update the state and rerender
|
|
60
|
+
state.layout = 'new';
|
|
61
|
+
state.description = 'description';
|
|
62
|
+
rerender();
|
|
63
|
+
|
|
64
|
+
expect(result.current().getState()).toEqual({
|
|
65
|
+
breadcrumbs: [],
|
|
66
|
+
sidebarData: [],
|
|
67
|
+
tableOfContents: [],
|
|
68
|
+
sharedConfig: {},
|
|
69
|
+
description: 'description',
|
|
70
|
+
layout: 'new',
|
|
71
|
+
route: undefined,
|
|
72
|
+
title: undefined
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe('WHEN executed on the server', () => {
|
|
78
|
+
test('THEN the store is always reinitialized', () => {
|
|
79
|
+
const state = { layout: 'layout', description: 'des' };
|
|
80
|
+
const { result, rerender } = renderHook(() => useCreateStore(state, true));
|
|
81
|
+
expect(result.current().getState()).toEqual({
|
|
82
|
+
breadcrumbs: [],
|
|
83
|
+
sidebarData: [],
|
|
84
|
+
tableOfContents: [],
|
|
85
|
+
sharedConfig: {},
|
|
86
|
+
...state,
|
|
87
|
+
route: undefined,
|
|
88
|
+
title: undefined
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// update the state and rerender
|
|
92
|
+
state.layout = 'new';
|
|
93
|
+
state.description = 'description';
|
|
94
|
+
rerender();
|
|
95
|
+
|
|
96
|
+
expect(result.current().getState()).toEqual({
|
|
97
|
+
breadcrumbs: [],
|
|
98
|
+
sidebarData: [],
|
|
99
|
+
tableOfContents: [],
|
|
100
|
+
sharedConfig: {},
|
|
101
|
+
description: 'description',
|
|
102
|
+
layout: 'new',
|
|
103
|
+
route: undefined,
|
|
104
|
+
title: undefined
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe('GIVEN the `useStore` hook', () => {
|
|
111
|
+
describe('WHEN a selector is provided', () => {
|
|
112
|
+
test('THEN the hook applies the selector and returns the selected state', () => {
|
|
113
|
+
const { result } = renderHook(() => useStore(state => state.title), {
|
|
114
|
+
wrapper: createWrapper({ title: 'title' })
|
|
115
|
+
});
|
|
116
|
+
expect(result.current).toEqual('title');
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
describe('WHEN rendered outside of the StoreContext', () => {
|
|
121
|
+
test('THEN the hook throws an error', () => {
|
|
122
|
+
const { result } = renderHook(() => useStore(state => state.title));
|
|
123
|
+
expect(result.error?.message).toBe('Missing StoreProvider in the tree');
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { render } from '@testing-library/react';
|
|
3
|
+
|
|
4
|
+
import { useCreateStore, StoreProvider, type SiteState } from '../../store';
|
|
5
|
+
|
|
6
|
+
export function renderWithStore(ui: React.ReactElement) {
|
|
7
|
+
const { rerender, ...result } = render(<Store>{ui}</Store>);
|
|
8
|
+
return {
|
|
9
|
+
...result,
|
|
10
|
+
rerender: (rerenderUi: React.ReactElement) => rerender(<Store>{rerenderUi}</Store>)
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function createWrapper(state?: Partial<SiteState>) {
|
|
15
|
+
return ({ children }: { children?: React.ReactNode }) => <Store state={state}>{children}</Store>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
*
|
|
20
|
+
* @param state: initial state for the site store
|
|
21
|
+
*/
|
|
22
|
+
const Store: React.FC<{ state?: Partial<SiteState> }> = ({ children, state = {} }) => {
|
|
23
|
+
const createStore = useCreateStore(state);
|
|
24
|
+
return <StoreProvider value={createStore()}>{children}</StoreProvider>;
|
|
25
|
+
};
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { renderHook } from '@testing-library/react-hooks';
|
|
2
|
+
|
|
3
|
+
import { createWrapper } from './test-utils/utils';
|
|
4
|
+
import { useAppHeader } from '../useAppHeader';
|
|
5
|
+
import { SiteState } from '../store';
|
|
6
|
+
|
|
7
|
+
const header: SiteState['sharedConfig']['header'] = {
|
|
8
|
+
homeLink: '/mosaic',
|
|
9
|
+
logo: '/some/url',
|
|
10
|
+
title: 'Digital Platform mosaic',
|
|
11
|
+
menu: [
|
|
12
|
+
{
|
|
13
|
+
title: 'Products',
|
|
14
|
+
link: '/mosaic/products'
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
title: 'Case Studies',
|
|
18
|
+
link: '/mosaic/case-studies'
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
title: 'Docs & Tools',
|
|
22
|
+
links: [
|
|
23
|
+
{
|
|
24
|
+
title: 'Docs',
|
|
25
|
+
link: '/mosaic/docs'
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
title: 'APIs',
|
|
29
|
+
link: '/mosaic/apis'
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
title: 'Start Your Project',
|
|
33
|
+
link: '/apps/app-registry'
|
|
34
|
+
}
|
|
35
|
+
]
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
title: 'Community Updates',
|
|
39
|
+
links: [
|
|
40
|
+
{
|
|
41
|
+
title: 'Newsletters',
|
|
42
|
+
link: '/mosaic/newsletters'
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
title: 'Blog',
|
|
46
|
+
link: ' http://www.example.com/blog'
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
title: 'Podcasts',
|
|
50
|
+
link: '/mosaic/podcasts'
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
title: 'Release Notes',
|
|
54
|
+
link: '/mosaic/release-notes'
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
title: 'Operating Model',
|
|
58
|
+
link: ' http://www.example.com/release-notes'
|
|
59
|
+
}
|
|
60
|
+
]
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
title: 'Support',
|
|
64
|
+
links: [
|
|
65
|
+
{
|
|
66
|
+
title: 'Contact',
|
|
67
|
+
link: '/mosaic/support'
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
title: 'Contributing',
|
|
71
|
+
link: '/mosaic/support/contributing'
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
title: 'Incidents & RCAs',
|
|
75
|
+
link: '/mosaic/support/rcas'
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
title: 'Runbooks',
|
|
79
|
+
link: '/mosaic/support/runbooks'
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
title: 'Site',
|
|
83
|
+
link: '/mosaic/site'
|
|
84
|
+
}
|
|
85
|
+
]
|
|
86
|
+
}
|
|
87
|
+
]
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const invalidMenuHeader: SiteState['sharedConfig']['header'] = {
|
|
91
|
+
homeLink: '/mosaic',
|
|
92
|
+
logo: '/some/url',
|
|
93
|
+
title: 'Mosaic',
|
|
94
|
+
menu: [
|
|
95
|
+
{
|
|
96
|
+
title: 'Products',
|
|
97
|
+
// @ts-ignore
|
|
98
|
+
href: '/mosaic/products'
|
|
99
|
+
}
|
|
100
|
+
]
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const state: Partial<SiteState> = {
|
|
104
|
+
sharedConfig: { header }
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const inValidMenuState: Partial<SiteState> = {
|
|
108
|
+
sharedConfig: { header: invalidMenuHeader }
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
describe('GIVEN the `useAppHeader` hook', () => {
|
|
112
|
+
describe('WHEN there are no header props in the store', () => {
|
|
113
|
+
test('THEN the hook returns undefined', () => {
|
|
114
|
+
const { result } = renderHook(() => useAppHeader(), {
|
|
115
|
+
wrapper: createWrapper()
|
|
116
|
+
});
|
|
117
|
+
expect(result.current).toBeUndefined();
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
describe('WHEN there are header props in the store', () => {
|
|
122
|
+
test('THEN the hook returns a homelink, logo and a title', () => {
|
|
123
|
+
const { result } = renderHook(() => useAppHeader(), {
|
|
124
|
+
wrapper: createWrapper({ ...state })
|
|
125
|
+
});
|
|
126
|
+
expect(result.current?.homeLink).toEqual(header.homeLink);
|
|
127
|
+
expect(result.current?.logo).toEqual(header.logo);
|
|
128
|
+
expect(result.current?.title).toEqual(header.title);
|
|
129
|
+
expect(result.current?.menu.length).toEqual(5);
|
|
130
|
+
});
|
|
131
|
+
test('AND THEN the menu items have been assigned a type', () => {
|
|
132
|
+
const { result } = renderHook(() => useAppHeader(), {
|
|
133
|
+
wrapper: createWrapper({ ...state })
|
|
134
|
+
});
|
|
135
|
+
expect(result.current?.menu.length).toEqual(5);
|
|
136
|
+
expect(result.current?.menu[0].type).toEqual('link');
|
|
137
|
+
expect(result.current?.menu[3].type).toEqual('menu');
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
describe('AND WHEN a search namespace is *NOT* provided', () => {
|
|
141
|
+
test('THEN the namespace endpoint env variable is used', () => {
|
|
142
|
+
const { result } = renderHook(() => useAppHeader(), {
|
|
143
|
+
wrapper: createWrapper({ ...state })
|
|
144
|
+
});
|
|
145
|
+
expect(result.current?.HeaderControlsProps?.searchEndpoint).toEqual(undefined);
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
describe('AND WHEN a search namespace is provided', () => {
|
|
150
|
+
beforeAll(() => {
|
|
151
|
+
header.searchNamespace = 'namespace';
|
|
152
|
+
process.env[`SEARCH_ENDPOINT_${header.searchNamespace}`] = 'namespace-endpoint';
|
|
153
|
+
});
|
|
154
|
+
afterAll(() => {
|
|
155
|
+
process.env[`SEARCH_ENDPOINT_${header.searchNamespace}`] = undefined;
|
|
156
|
+
});
|
|
157
|
+
test('THEN the SEARCH_ENDPOINT env variable is used', () => {
|
|
158
|
+
const { result } = renderHook(() => useAppHeader(), {
|
|
159
|
+
wrapper: createWrapper({ ...state })
|
|
160
|
+
});
|
|
161
|
+
expect(result.current?.HeaderControlsProps?.searchEndpoint).toEqual('namespace-endpoint');
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
describe('AND WHEN invalid menu items are provided', () => {
|
|
166
|
+
let consoleError;
|
|
167
|
+
beforeAll(() => {
|
|
168
|
+
consoleError = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
169
|
+
});
|
|
170
|
+
afterAll(() => {
|
|
171
|
+
jest.restoreAllMocks();
|
|
172
|
+
});
|
|
173
|
+
test('THEN no menu items are returned', () => {
|
|
174
|
+
const { result } = renderHook(() => useAppHeader(), {
|
|
175
|
+
wrapper: createWrapper({ ...inValidMenuState })
|
|
176
|
+
});
|
|
177
|
+
expect(result.current?.menu.length).toEqual(0);
|
|
178
|
+
expect(consoleError).toHaveBeenCalledTimes(1);
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { renderHook } from '@testing-library/react-hooks';
|
|
2
|
+
|
|
3
|
+
import { createWrapper } from './test-utils/utils';
|
|
4
|
+
import { type Breadcrumb, useBreadcrumbs } from '../useBreadcrumbs';
|
|
5
|
+
|
|
6
|
+
const breadcrumbs: Breadcrumb[] = [
|
|
7
|
+
{ label: 'Home', path: '/home', hidden: false, id: '1' },
|
|
8
|
+
{ label: 'Docs', path: '/docs', hidden: false, id: '2' },
|
|
9
|
+
{ label: 'Products', path: '/products', hidden: false, id: '3' }
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
describe('GIVEN the `useBreadcrumbs` hook', () => {
|
|
13
|
+
describe('WHEN there are NO breadcrumbs are in the store', () => {
|
|
14
|
+
test('THEN the hook returns an empty collection', () => {
|
|
15
|
+
const { result } = renderHook(() => useBreadcrumbs(), {
|
|
16
|
+
wrapper: createWrapper()
|
|
17
|
+
});
|
|
18
|
+
expect(result.current.breadcrumbs).toEqual([]);
|
|
19
|
+
expect(result.current.enabled).toEqual(false);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe('WHEN there are breadcrumbs are in the store', () => {
|
|
24
|
+
test('THEN the hook returns those breadcrumbs', () => {
|
|
25
|
+
const { result } = renderHook(() => useBreadcrumbs(), {
|
|
26
|
+
wrapper: createWrapper({ breadcrumbs })
|
|
27
|
+
});
|
|
28
|
+
expect(result.current.breadcrumbs.length).toEqual(3);
|
|
29
|
+
expect(result.current.enabled).toEqual(true);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
describe('WHEN there are breadcrumbs are in the store', () => {
|
|
34
|
+
test('THEN enabled is false if there are not enough breadcrumbs', () => {
|
|
35
|
+
const { result } = renderHook(() => useBreadcrumbs(10), {
|
|
36
|
+
wrapper: createWrapper({ breadcrumbs })
|
|
37
|
+
});
|
|
38
|
+
expect(result.current.breadcrumbs.length).toEqual(3);
|
|
39
|
+
expect(result.current.enabled).toEqual(false);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { renderHook } from '@testing-library/react-hooks';
|
|
2
|
+
|
|
3
|
+
import { createWrapper } from './test-utils/utils';
|
|
4
|
+
import { useFooter } from '../useFooter';
|
|
5
|
+
import { SiteState } from '../store';
|
|
6
|
+
|
|
7
|
+
const footer: SiteState['sharedConfig']['footer'] = {
|
|
8
|
+
description: 'Sign up to stay on top of new developments and solutions.',
|
|
9
|
+
title: 'Subscribe to our Newsletter',
|
|
10
|
+
href: 'http://a.link.com',
|
|
11
|
+
label: 'Sign Up',
|
|
12
|
+
helpLinks: {
|
|
13
|
+
stackoverflowLabel: 'View our Stack Overflow',
|
|
14
|
+
stackoverflowUrl: 'https://stack.com',
|
|
15
|
+
symphonyLabel: 'Chat on Symphony',
|
|
16
|
+
symphonyUrl: 'symphony://a_stream'
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const invalidMenuHeader: SiteState['sharedConfig']['header'] = {
|
|
21
|
+
homeLink: '/developer',
|
|
22
|
+
logo: '/some/url',
|
|
23
|
+
title: 'Digital Platform Developer',
|
|
24
|
+
menu: [
|
|
25
|
+
{
|
|
26
|
+
title: 'Products',
|
|
27
|
+
// @ts-ignore
|
|
28
|
+
href: '/developer/products'
|
|
29
|
+
}
|
|
30
|
+
]
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const state: Partial<SiteState> = {
|
|
34
|
+
sharedConfig: { footer }
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
describe('GIVEN the `useFooter` hook', () => {
|
|
38
|
+
describe('WHEN there are no footer props in the store', () => {
|
|
39
|
+
test('THEN the hook returns undefined', () => {
|
|
40
|
+
const { result } = renderHook(() => useFooter(), {
|
|
41
|
+
wrapper: createWrapper()
|
|
42
|
+
});
|
|
43
|
+
expect(result.current).toBeUndefined();
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe('WHEN there are footer props in the store', () => {
|
|
48
|
+
test('THEN the footer props are returned', () => {
|
|
49
|
+
const { result } = renderHook(() => useFooter(), {
|
|
50
|
+
wrapper: createWrapper({ ...state })
|
|
51
|
+
});
|
|
52
|
+
expect(result.current?.description).toEqual(footer.description);
|
|
53
|
+
expect(result.current?.title).toEqual(footer.title);
|
|
54
|
+
expect(result.current?.href).toEqual(footer.href);
|
|
55
|
+
expect(result.current?.label).toEqual(footer.label);
|
|
56
|
+
expect(result.current?.helpLinks.stackoverflowLabel).toEqual(
|
|
57
|
+
footer.helpLinks.stackoverflowLabel
|
|
58
|
+
);
|
|
59
|
+
expect(result.current?.helpLinks.stackoverflowUrl).toEqual(footer.helpLinks.stackoverflowUrl);
|
|
60
|
+
expect(result.current?.helpLinks.symphonyLabel).toEqual(footer.helpLinks.symphonyLabel);
|
|
61
|
+
expect(result.current?.helpLinks.symphonyUrl).toEqual(footer.helpLinks.symphonyUrl);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { renderHook } from '@testing-library/react-hooks';
|
|
2
|
+
|
|
3
|
+
import { createWrapper } from './test-utils/utils';
|
|
4
|
+
import { useLayout } from '../useLayout';
|
|
5
|
+
|
|
6
|
+
describe('GIVEN the `useLayout` hook', () => {
|
|
7
|
+
describe('WHEN there is no layout in the store', () => {
|
|
8
|
+
test('THEN the hook returns undefined', () => {
|
|
9
|
+
const { result } = renderHook(() => useLayout(), {
|
|
10
|
+
wrapper: createWrapper()
|
|
11
|
+
});
|
|
12
|
+
expect(result.current.layout).toBeUndefined();
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
describe('WHEN there is a layout in the store', () => {
|
|
17
|
+
test('THEN the hook returns the layout', () => {
|
|
18
|
+
const { result } = renderHook(() => useLayout(), {
|
|
19
|
+
wrapper: createWrapper({ layout: 'LayoutName' })
|
|
20
|
+
});
|
|
21
|
+
expect(result.current.layout).toEqual('LayoutName');
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { renderHook } from '@testing-library/react-hooks';
|
|
2
|
+
|
|
3
|
+
import { createWrapper } from './test-utils/utils';
|
|
4
|
+
import { useMeta } from '../useMeta';
|
|
5
|
+
import { Breadcrumb } from '../types';
|
|
6
|
+
|
|
7
|
+
const breadcrumbs: Breadcrumb[] = [
|
|
8
|
+
{ label: 'Home', path: '/home', hidden: false, id: '1' },
|
|
9
|
+
{ label: 'Docs', path: '/docs', hidden: false, id: '2' },
|
|
10
|
+
{ label: 'Products', path: '/products', hidden: false, id: '3' }
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
describe('GIVEN the `useMeta` hook', () => {
|
|
14
|
+
describe('WHEN there is no metadata in the store', () => {
|
|
15
|
+
test('THEN the hook returns no metadata', () => {
|
|
16
|
+
const { result } = renderHook(() => useMeta(), {
|
|
17
|
+
wrapper: createWrapper()
|
|
18
|
+
});
|
|
19
|
+
expect(result.current.meta.breadcrumbs).toEqual([]);
|
|
20
|
+
expect(result.current.meta.description).toBeUndefined();
|
|
21
|
+
expect(result.current.meta.title).toBeUndefined();
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
describe('WHEN there is metadata in the store', () => {
|
|
26
|
+
test('THEN the hook returns the metadata', () => {
|
|
27
|
+
const { result } = renderHook(() => useMeta(), {
|
|
28
|
+
wrapper: createWrapper({ breadcrumbs, title: 'A Title', description: 'A Description' })
|
|
29
|
+
});
|
|
30
|
+
expect(result.current.meta.breadcrumbs).toEqual(breadcrumbs);
|
|
31
|
+
expect(result.current.meta.title).toEqual('A Title');
|
|
32
|
+
expect(result.current.meta.description).toEqual('A Description');
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { renderHook } from '@testing-library/react-hooks';
|
|
2
|
+
|
|
3
|
+
import { createWrapper } from './test-utils/utils';
|
|
4
|
+
import { useRoute } from '../useRoute';
|
|
5
|
+
|
|
6
|
+
describe('GIVEN the `useRoute` hook', () => {
|
|
7
|
+
describe('WHEN there is no route in the store', () => {
|
|
8
|
+
test('THEN the hook returns no route', () => {
|
|
9
|
+
const { result } = renderHook(() => useRoute(), {
|
|
10
|
+
wrapper: createWrapper()
|
|
11
|
+
});
|
|
12
|
+
expect(result.current.route).toBeUndefined();
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
describe('WHEN there is a route in the store', () => {
|
|
17
|
+
test('THEN the hook returns the route', () => {
|
|
18
|
+
const { result } = renderHook(() => useRoute(), {
|
|
19
|
+
wrapper: createWrapper({ route: 'a/route/to/a/page' })
|
|
20
|
+
});
|
|
21
|
+
expect(result.current.route).toEqual('a/route/to/a/page');
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { renderHook } from '@testing-library/react-hooks';
|
|
2
|
+
|
|
3
|
+
import { createWrapper } from './test-utils/utils';
|
|
4
|
+
import { TableOfContentsItem, useTableOfContents } from '../useTableOfContents';
|
|
5
|
+
|
|
6
|
+
const tableOfContents: TableOfContentsItem[] = [
|
|
7
|
+
{ level: 2, id: 'what-is-analytics', text: 'What is Analytics' },
|
|
8
|
+
{ level: 2, id: 'what-is-our-goal', text: 'What is our goal?' },
|
|
9
|
+
{ level: 2, id: 'benefits-of-using-our-service', text: 'Benefits of using our Service' },
|
|
10
|
+
{ level: 2, id: 'useful-go-links', text: 'Useful Go links' }
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
describe('GIVEN the `useTableOfContents` hook', () => {
|
|
14
|
+
describe('WHEN there is NO table of contents in the store', () => {
|
|
15
|
+
test('THEN the hook returns an empty collection', () => {
|
|
16
|
+
const { result } = renderHook(() => useTableOfContents(), {
|
|
17
|
+
wrapper: createWrapper()
|
|
18
|
+
});
|
|
19
|
+
expect(result.current.tableOfContents).toEqual([]);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe('WHEN table of contents is in the store', () => {
|
|
24
|
+
test('THEN the hook returns the tabel of contents', () => {
|
|
25
|
+
const { result } = renderHook(() => useTableOfContents(), {
|
|
26
|
+
wrapper: createWrapper({ tableOfContents })
|
|
27
|
+
});
|
|
28
|
+
expect(result.current.tableOfContents.length).toEqual(4);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from './store';
|
|
2
|
+
export * from './useAppHeader';
|
|
3
|
+
export * from './useBreadcrumbs';
|
|
4
|
+
export * from './useLayout';
|
|
5
|
+
export * from './useMeta';
|
|
6
|
+
export * from './useNextPrev';
|
|
7
|
+
export * from './useRoute';
|
|
8
|
+
export * from './useSidebar';
|
|
9
|
+
export * from './useTableOfContents';
|
|
10
|
+
export * from './useFooter';
|
|
11
|
+
|
|
12
|
+
export * from './types';
|
|
13
|
+
export { MenuItemType } from './types';
|
package/src/store.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { useLayoutEffect, createContext, useContext } from 'react';
|
|
2
|
+
import { createStore, StoreApi, useStore as useZustandStore } from 'zustand';
|
|
3
|
+
|
|
4
|
+
import { devtools } from 'zustand/middleware';
|
|
5
|
+
|
|
6
|
+
import type { BreadcrumbsSlice } from './types/breadcrumbs';
|
|
7
|
+
import type { SharedConfigSlice } from './types/sharedConfig';
|
|
8
|
+
import type { LayoutSlice } from './types/layout';
|
|
9
|
+
import type { SidebarSlice } from './types/sidebar';
|
|
10
|
+
import type { TableOfContentsSlice } from './types/tableOfContents';
|
|
11
|
+
|
|
12
|
+
let store: StoreApi<SiteState>;
|
|
13
|
+
|
|
14
|
+
export type SiteState = BreadcrumbsSlice &
|
|
15
|
+
LayoutSlice &
|
|
16
|
+
SidebarSlice &
|
|
17
|
+
TableOfContentsSlice &
|
|
18
|
+
SharedConfigSlice & {
|
|
19
|
+
/** Page metadata description, used by search */
|
|
20
|
+
description?: string;
|
|
21
|
+
/** Page route */
|
|
22
|
+
route?: string;
|
|
23
|
+
/** Page title */
|
|
24
|
+
title?: string;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
function getDefaultInitialState(): SiteState {
|
|
28
|
+
return {
|
|
29
|
+
breadcrumbs: [],
|
|
30
|
+
sidebarData: [],
|
|
31
|
+
tableOfContents: [],
|
|
32
|
+
sharedConfig: {},
|
|
33
|
+
description: undefined,
|
|
34
|
+
layout: undefined,
|
|
35
|
+
route: undefined,
|
|
36
|
+
title: undefined
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const StoreContext = createContext<typeof store | undefined>(undefined);
|
|
41
|
+
const StoreProvider = StoreContext.Provider;
|
|
42
|
+
|
|
43
|
+
const initializeStore = (preloadedState: Partial<SiteState> = {}) =>
|
|
44
|
+
createStore(
|
|
45
|
+
devtools<SiteState>(() => ({
|
|
46
|
+
...getDefaultInitialState(),
|
|
47
|
+
...preloadedState
|
|
48
|
+
}))
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
function useCreateStore(serverInitialState: Partial<SiteState>, isSSR: boolean = false) {
|
|
52
|
+
// Server side code: For SSR & SSG, always use a new store.
|
|
53
|
+
if (typeof window === 'undefined' || isSSR) {
|
|
54
|
+
return () => initializeStore(serverInitialState);
|
|
55
|
+
}
|
|
56
|
+
// End of server side code
|
|
57
|
+
|
|
58
|
+
// Client side code:
|
|
59
|
+
// Next.js always re-uses same store regardless of whether page is a SSR or SSG or CSR type.
|
|
60
|
+
const isReusingStore = Boolean(store);
|
|
61
|
+
store = store ?? initializeStore(serverInitialState);
|
|
62
|
+
|
|
63
|
+
// When next.js re-renders _app while re-using an older store, then replace current state with
|
|
64
|
+
// the new state (in the next render cycle).
|
|
65
|
+
// (Why next render cycle? Because react cannot re-render while a render is already in progress.
|
|
66
|
+
// i.e. we cannot do a setState() as that will initiate a re-render)
|
|
67
|
+
//
|
|
68
|
+
// eslint complaining "React Hooks must be called in the exact same order in every component render"
|
|
69
|
+
// is ignorable as this code runs in same order in a given environment (i.e. client or server)
|
|
70
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
71
|
+
useLayoutEffect(() => {
|
|
72
|
+
// serverInitialState is undefined for CSR pages. It is up to you if you want to reset
|
|
73
|
+
// states on CSR page navigation or not. I have chosen not to, but if you choose to,
|
|
74
|
+
// then add `serverInitialState = getDefaultInitialState()` here.
|
|
75
|
+
if (serverInitialState && isReusingStore) {
|
|
76
|
+
store.setState(
|
|
77
|
+
{
|
|
78
|
+
// re-use functions from existing store
|
|
79
|
+
...store.getState(),
|
|
80
|
+
// but reset all other properties.
|
|
81
|
+
...serverInitialState
|
|
82
|
+
},
|
|
83
|
+
true // replace states, rather than shallow merging
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
return () => store;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Hook providing access to state stored in the site store
|
|
93
|
+
*/
|
|
94
|
+
function useStore<T>(
|
|
95
|
+
selector: (state: SiteState) => T,
|
|
96
|
+
equalityFn?: (left: T, right: T) => boolean
|
|
97
|
+
): T {
|
|
98
|
+
const store = useContext(StoreContext);
|
|
99
|
+
if (!store) {
|
|
100
|
+
throw new Error('Missing StoreProvider in the tree');
|
|
101
|
+
}
|
|
102
|
+
return useZustandStore(store, selector, equalityFn);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export { useCreateStore, StoreProvider, useStore, initializeStore };
|