@cccsaurora/howler-ui 3.1.0-dev.1618 → 3.1.0-dev.1621
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 +1 -1
- package/plugins/clue/Provider.d.ts +16 -1
- package/plugins/clue/Provider.js +29 -3
- package/plugins/clue/Provider.test.d.ts +1 -0
- package/plugins/clue/Provider.test.js +119 -0
- package/plugins/clue/index.d.ts +6 -3
- package/plugins/clue/index.js +8 -2
- package/plugins/clue/setup.js +3 -3
- package/plugins/clue/setup.test.d.ts +1 -0
- package/plugins/clue/setup.test.js +64 -0
package/package.json
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
import type { DatabaseConfig } from '@cccsaurora/clue-ui/database/types';
|
|
1
2
|
import { type PropsWithChildren } from 'react';
|
|
2
|
-
|
|
3
|
+
export interface CluePluginOptions {
|
|
4
|
+
baseURL?: string;
|
|
5
|
+
getToken?: () => string;
|
|
6
|
+
publicIconify?: boolean;
|
|
7
|
+
customIconify?: string;
|
|
8
|
+
includeContext?: boolean;
|
|
9
|
+
defaultTimeout?: number;
|
|
10
|
+
chunkSize?: number;
|
|
11
|
+
replicate?: boolean;
|
|
12
|
+
storageType?: DatabaseConfig['storageType'];
|
|
13
|
+
}
|
|
14
|
+
type ProviderProps = PropsWithChildren<{
|
|
15
|
+
options?: CluePluginOptions;
|
|
16
|
+
}>;
|
|
17
|
+
declare const Provider: React.FC<ProviderProps>;
|
|
3
18
|
export default Provider;
|
package/plugins/clue/Provider.js
CHANGED
|
@@ -1,13 +1,39 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { buildDatabase } from '@cccsaurora/clue-ui';
|
|
2
3
|
import { ClueProvider } from '@cccsaurora/clue-ui/hooks/ClueProvider';
|
|
3
4
|
import { ApiConfigContext } from '@cccsaurora/howler-ui/components/app/providers/ApiConfigProvider';
|
|
4
|
-
import { useContext } from 'react';
|
|
5
|
+
import { useCallback, useContext, useEffect, useState } from 'react';
|
|
5
6
|
import { useTranslation } from 'react-i18next';
|
|
6
7
|
import { StorageKey } from '@cccsaurora/howler-ui/utils/constants';
|
|
7
8
|
import { getStored } from '@cccsaurora/howler-ui/utils/localStorage';
|
|
8
|
-
const Provider = ({ children }) => {
|
|
9
|
+
const Provider = ({ children, options = {} }) => {
|
|
9
10
|
const { config } = useContext(ApiConfigContext);
|
|
10
11
|
const features = config?.configuration?.features ?? {};
|
|
11
|
-
|
|
12
|
+
const [database, setDatabase] = useState();
|
|
13
|
+
const defaultGetToken = useCallback(() => getStored(StorageKey.APP_TOKEN) ?? '', []);
|
|
14
|
+
const baseURL = options.baseURL ?? `${location.origin}/api/v1/clue`;
|
|
15
|
+
const getToken = options.getToken ?? defaultGetToken;
|
|
16
|
+
const publicIconify = options.publicIconify ?? location.origin.includes('localhost');
|
|
17
|
+
const customIconify = options.customIconify ?? location.origin.replace('howler', 'icons');
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
let cancelled = false;
|
|
20
|
+
if (!features.clue) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
void buildDatabase({
|
|
24
|
+
storageType: options.storageType ?? 'memory',
|
|
25
|
+
replicate: !!options.replicate,
|
|
26
|
+
baseURL,
|
|
27
|
+
getToken
|
|
28
|
+
}).then(database => {
|
|
29
|
+
if (!cancelled) {
|
|
30
|
+
setDatabase(database);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
return () => {
|
|
34
|
+
cancelled = true;
|
|
35
|
+
};
|
|
36
|
+
}, [baseURL, features.clue, getToken, options.storageType, options.replicate]);
|
|
37
|
+
return (_jsx(ClueProvider, { baseURL: baseURL, getToken: getToken, enabled: features.clue, publicIconify: publicIconify, customIconify: customIconify, defaultTimeout: options.defaultTimeout ?? 5, i18next: useTranslation('clue'), chunkSize: options.chunkSize ?? 50, database: database, includeContext: options.includeContext, children: children }));
|
|
12
38
|
};
|
|
13
39
|
export default Provider;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { act, render, waitFor } from '@testing-library/react';
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
4
|
+
const mocks = vi.hoisted(() => ({
|
|
5
|
+
buildDatabase: vi.fn(),
|
|
6
|
+
clueProviderProps: undefined,
|
|
7
|
+
token: 'stored-token'
|
|
8
|
+
}));
|
|
9
|
+
vi.mock('@cccsaurora/clue-ui', () => ({
|
|
10
|
+
buildDatabase: mocks.buildDatabase
|
|
11
|
+
}));
|
|
12
|
+
vi.mock('@cccsaurora/clue-ui/hooks/ClueProvider', () => ({
|
|
13
|
+
ClueProvider: ({ children, ...props }) => {
|
|
14
|
+
mocks.clueProviderProps = props;
|
|
15
|
+
return _jsx(_Fragment, { children: children });
|
|
16
|
+
}
|
|
17
|
+
}));
|
|
18
|
+
vi.mock('components/app/providers/ApiConfigProvider', async () => {
|
|
19
|
+
const { createContext } = await import('react');
|
|
20
|
+
return { ApiConfigContext: createContext({ config: {} }) };
|
|
21
|
+
});
|
|
22
|
+
vi.mock('react-i18next', () => ({
|
|
23
|
+
useTranslation: () => ({ i18n: { language: 'en' } })
|
|
24
|
+
}));
|
|
25
|
+
vi.mock('utils/constants', () => ({
|
|
26
|
+
StorageKey: { APP_TOKEN: 'app_token' }
|
|
27
|
+
}));
|
|
28
|
+
vi.mock('utils/localStorage', () => ({
|
|
29
|
+
getStored: () => mocks.token
|
|
30
|
+
}));
|
|
31
|
+
import { ApiConfigContext } from '@cccsaurora/howler-ui/components/app/providers/ApiConfigProvider';
|
|
32
|
+
import Provider, {} from './Provider';
|
|
33
|
+
const getProviderProps = () => {
|
|
34
|
+
if (!mocks.clueProviderProps) {
|
|
35
|
+
throw new Error('ClueProvider was not rendered');
|
|
36
|
+
}
|
|
37
|
+
return mocks.clueProviderProps;
|
|
38
|
+
};
|
|
39
|
+
const renderProvider = (config, options) => render(_jsx(ApiConfigContext.Provider, { value: { config: config, setConfig: vi.fn(), loaded: true }, children: _jsx(Provider, { options: options, children: "content" }) }));
|
|
40
|
+
describe('Clue Provider', () => {
|
|
41
|
+
beforeEach(() => {
|
|
42
|
+
mocks.buildDatabase.mockReset();
|
|
43
|
+
mocks.clueProviderProps = undefined;
|
|
44
|
+
mocks.token = 'stored-token';
|
|
45
|
+
});
|
|
46
|
+
afterEach(() => {
|
|
47
|
+
vi.restoreAllMocks();
|
|
48
|
+
});
|
|
49
|
+
it('does not start replication while Clue is disabled and returns an empty absent token', () => {
|
|
50
|
+
mocks.token = null;
|
|
51
|
+
renderProvider({ configuration: { features: { clue: false } } });
|
|
52
|
+
expect(mocks.buildDatabase).not.toHaveBeenCalled();
|
|
53
|
+
expect(getProviderProps().enabled).toBe(false);
|
|
54
|
+
expect(getProviderProps().database).toBeUndefined();
|
|
55
|
+
expect(getProviderProps().getToken?.()).toBe('');
|
|
56
|
+
});
|
|
57
|
+
it('returns the current stored application token by default', () => {
|
|
58
|
+
renderProvider({ configuration: { features: { clue: false } } });
|
|
59
|
+
expect(getProviderProps().getToken?.()).toBe('stored-token');
|
|
60
|
+
});
|
|
61
|
+
it('builds a configured database and passes supplied deployment options', async () => {
|
|
62
|
+
const database = { name: 'replicated' };
|
|
63
|
+
const getToken = vi.fn(() => 'custom-token');
|
|
64
|
+
mocks.buildDatabase.mockResolvedValue(database);
|
|
65
|
+
renderProvider({ configuration: { features: { clue: true } } }, {
|
|
66
|
+
baseURL: 'https://howler.example/api/v1/clue',
|
|
67
|
+
getToken,
|
|
68
|
+
publicIconify: false,
|
|
69
|
+
customIconify: 'https://icons.example',
|
|
70
|
+
includeContext: true,
|
|
71
|
+
defaultTimeout: 7,
|
|
72
|
+
chunkSize: 25,
|
|
73
|
+
replicate: true,
|
|
74
|
+
storageType: 'memory'
|
|
75
|
+
});
|
|
76
|
+
await waitFor(() => {
|
|
77
|
+
expect(mocks.buildDatabase).toHaveBeenCalledWith({
|
|
78
|
+
storageType: 'memory',
|
|
79
|
+
replicate: true,
|
|
80
|
+
baseURL: 'https://howler.example/api/v1/clue',
|
|
81
|
+
getToken
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
expect(getProviderProps()).toMatchObject({
|
|
85
|
+
baseURL: 'https://howler.example/api/v1/clue',
|
|
86
|
+
database,
|
|
87
|
+
enabled: true,
|
|
88
|
+
publicIconify: false,
|
|
89
|
+
customIconify: 'https://icons.example',
|
|
90
|
+
includeContext: true,
|
|
91
|
+
defaultTimeout: 7,
|
|
92
|
+
chunkSize: 25
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
it('uses non-replicated in-memory storage by default', async () => {
|
|
96
|
+
mocks.buildDatabase.mockResolvedValue({ name: 'local' });
|
|
97
|
+
renderProvider({ configuration: { features: { clue: true } } });
|
|
98
|
+
await waitFor(() => {
|
|
99
|
+
expect(mocks.buildDatabase).toHaveBeenCalledWith({
|
|
100
|
+
storageType: 'memory',
|
|
101
|
+
replicate: false,
|
|
102
|
+
baseURL: `${location.origin}/api/v1/clue`,
|
|
103
|
+
getToken: expect.any(Function)
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
it('ignores a database that resolves after the provider unmounts', async () => {
|
|
108
|
+
let resolveDatabase = () => { };
|
|
109
|
+
mocks.buildDatabase.mockReturnValue(new Promise(resolve => {
|
|
110
|
+
resolveDatabase = resolve;
|
|
111
|
+
}));
|
|
112
|
+
const { unmount } = renderProvider({ configuration: { features: { clue: true } } });
|
|
113
|
+
unmount();
|
|
114
|
+
await act(async () => {
|
|
115
|
+
resolveDatabase({ name: 'late' });
|
|
116
|
+
});
|
|
117
|
+
expect(getProviderProps().database).toBeUndefined();
|
|
118
|
+
});
|
|
119
|
+
});
|
package/plugins/clue/index.d.ts
CHANGED
|
@@ -3,16 +3,19 @@ import type { PluginChipProps } from '@cccsaurora/howler-ui/components/elements/
|
|
|
3
3
|
import type { PluginTypographyProps } from '@cccsaurora/howler-ui/components/elements/PluginTypography';
|
|
4
4
|
import type { i18n as I18N } from 'i18next';
|
|
5
5
|
import HowlerPlugin from '@cccsaurora/howler-ui/plugins/HowlerPlugin';
|
|
6
|
+
import type { FC, PropsWithChildren } from 'react';
|
|
7
|
+
import { type CluePluginOptions } from './Provider';
|
|
6
8
|
import useSetup from './setup';
|
|
9
|
+
export type { CluePluginOptions } from './Provider';
|
|
7
10
|
declare class CluePlugin extends HowlerPlugin {
|
|
11
|
+
private readonly options;
|
|
12
|
+
constructor(options?: CluePluginOptions);
|
|
8
13
|
name: string;
|
|
9
14
|
version: string;
|
|
10
15
|
author: string;
|
|
11
16
|
description: string;
|
|
12
17
|
activate(): void;
|
|
13
|
-
provider():
|
|
14
|
-
children?: import("react").ReactNode | undefined;
|
|
15
|
-
}>;
|
|
18
|
+
provider(): FC<PropsWithChildren>;
|
|
16
19
|
setup(): typeof useSetup;
|
|
17
20
|
localization(i18nInstance: I18N): void;
|
|
18
21
|
helpers(): HowlerHelper[];
|
package/plugins/clue/index.js
CHANGED
|
@@ -13,9 +13,14 @@ import ClueTypography from './components/ClueTypography';
|
|
|
13
13
|
import HELPERS from './helpers';
|
|
14
14
|
import howlerClueEN from './locales/clue.en.json';
|
|
15
15
|
import howlerClueFR from './locales/clue.fr.json';
|
|
16
|
-
import Provider from './Provider';
|
|
16
|
+
import Provider, {} from './Provider';
|
|
17
17
|
import useSetup from './setup';
|
|
18
18
|
class CluePlugin extends HowlerPlugin {
|
|
19
|
+
options;
|
|
20
|
+
constructor(options = {}) {
|
|
21
|
+
super();
|
|
22
|
+
this.options = options;
|
|
23
|
+
}
|
|
19
24
|
name = 'CluePlugin';
|
|
20
25
|
version = '0.0.1';
|
|
21
26
|
author = 'Matthew Rafuse <matthew.rafuse@cyber.gc.ca>';
|
|
@@ -41,7 +46,8 @@ class CluePlugin extends HowlerPlugin {
|
|
|
41
46
|
super.addPivot('clue', pivotForm, pivotRenderer);
|
|
42
47
|
}
|
|
43
48
|
provider() {
|
|
44
|
-
|
|
49
|
+
const options = this.options;
|
|
50
|
+
return ({ children }) => _jsx(Provider, { options: options, children: children });
|
|
45
51
|
}
|
|
46
52
|
setup() {
|
|
47
53
|
return useSetup;
|
package/plugins/clue/setup.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { useAppUser } from '@tui/core';
|
|
2
1
|
import { SNACKBAR_EVENT_ID } from '@cccsaurora/clue-ui/data/event';
|
|
3
2
|
import useClue from '@cccsaurora/clue-ui/hooks/useClue';
|
|
3
|
+
import { useAppUser } from '@tui/core';
|
|
4
4
|
import { ApiConfigContext } from '@cccsaurora/howler-ui/components/app/providers/ApiConfigProvider';
|
|
5
5
|
import useMySnackbar from '@cccsaurora/howler-ui/components/hooks/useMySnackbar';
|
|
6
6
|
import { useContext, useEffect } from 'react';
|
|
@@ -9,7 +9,7 @@ const useSetup = () => {
|
|
|
9
9
|
const appUser = useAppUser();
|
|
10
10
|
const { config } = useContext(ApiConfigContext);
|
|
11
11
|
const { showSuccessMessage, showErrorMessage, showInfoMessage, showWarningMessage } = useMySnackbar();
|
|
12
|
-
const features = config
|
|
12
|
+
const features = config?.configuration?.features ?? {};
|
|
13
13
|
useEffect(() => {
|
|
14
14
|
// eslint-disable-next-line no-console
|
|
15
15
|
console.debug('Initializing clue snackbar event handler');
|
|
@@ -37,7 +37,7 @@ const useSetup = () => {
|
|
|
37
37
|
if (!appUser.isReady()) {
|
|
38
38
|
return;
|
|
39
39
|
}
|
|
40
|
-
if (features.
|
|
40
|
+
if (features.clue) {
|
|
41
41
|
clue.setReady(true);
|
|
42
42
|
}
|
|
43
43
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { renderHook } from '@testing-library/react';
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
4
|
+
const mocks = vi.hoisted(() => ({
|
|
5
|
+
appUserReady: true,
|
|
6
|
+
setReady: vi.fn(),
|
|
7
|
+
snackbars: {
|
|
8
|
+
showErrorMessage: vi.fn(),
|
|
9
|
+
showInfoMessage: vi.fn(),
|
|
10
|
+
showSuccessMessage: vi.fn(),
|
|
11
|
+
showWarningMessage: vi.fn()
|
|
12
|
+
}
|
|
13
|
+
}));
|
|
14
|
+
vi.mock('@tui/core', () => ({
|
|
15
|
+
useAppUser: () => ({ isReady: () => mocks.appUserReady })
|
|
16
|
+
}));
|
|
17
|
+
vi.mock('@cccsaurora/clue-ui/data/event', () => ({
|
|
18
|
+
SNACKBAR_EVENT_ID: 'clue.snackbar'
|
|
19
|
+
}));
|
|
20
|
+
vi.mock('@cccsaurora/clue-ui/hooks/useClue', () => ({
|
|
21
|
+
default: () => ({ setReady: mocks.setReady })
|
|
22
|
+
}));
|
|
23
|
+
vi.mock('components/app/providers/ApiConfigProvider', async () => {
|
|
24
|
+
const { createContext } = await import('react');
|
|
25
|
+
return { ApiConfigContext: createContext({ config: {} }) };
|
|
26
|
+
});
|
|
27
|
+
vi.mock('components/hooks/useMySnackbar', () => ({
|
|
28
|
+
default: () => mocks.snackbars
|
|
29
|
+
}));
|
|
30
|
+
import { ApiConfigContext } from '@cccsaurora/howler-ui/components/app/providers/ApiConfigProvider';
|
|
31
|
+
import useSetup from './setup';
|
|
32
|
+
const setupWrapper = (config) => ({ children }) => (_jsx(ApiConfigContext.Provider, { value: { config: config, setConfig: vi.fn(), loaded: true }, children: children }));
|
|
33
|
+
describe('Clue setup', () => {
|
|
34
|
+
beforeEach(() => {
|
|
35
|
+
mocks.appUserReady = true;
|
|
36
|
+
mocks.setReady.mockReset();
|
|
37
|
+
Object.values(mocks.snackbars).forEach(snackbar => snackbar.mockReset());
|
|
38
|
+
});
|
|
39
|
+
afterEach(() => {
|
|
40
|
+
vi.restoreAllMocks();
|
|
41
|
+
});
|
|
42
|
+
it('routes snackbar events and removes the event listener on cleanup', () => {
|
|
43
|
+
const { unmount } = renderHook(() => useSetup(), {
|
|
44
|
+
wrapper: setupWrapper({ configuration: { features: { clue: true } } })
|
|
45
|
+
});
|
|
46
|
+
window.dispatchEvent(new CustomEvent('clue.snackbar', { detail: { level: 'success', message: 'done' } }));
|
|
47
|
+
expect(mocks.snackbars.showSuccessMessage).toHaveBeenCalledWith('done', undefined, undefined);
|
|
48
|
+
expect(mocks.setReady).toHaveBeenCalledWith(true);
|
|
49
|
+
unmount();
|
|
50
|
+
window.dispatchEvent(new CustomEvent('clue.snackbar', { detail: { level: 'error', message: 'ignored' } }));
|
|
51
|
+
expect(mocks.snackbars.showErrorMessage).not.toHaveBeenCalled();
|
|
52
|
+
});
|
|
53
|
+
it('does not enable readiness for Borealis alone or before the app user is ready', () => {
|
|
54
|
+
renderHook(() => useSetup(), {
|
|
55
|
+
wrapper: setupWrapper({ configuration: { features: { borealis: true } } })
|
|
56
|
+
});
|
|
57
|
+
expect(mocks.setReady).not.toHaveBeenCalled();
|
|
58
|
+
mocks.appUserReady = false;
|
|
59
|
+
renderHook(() => useSetup(), {
|
|
60
|
+
wrapper: setupWrapper({ configuration: { features: { clue: true } } })
|
|
61
|
+
});
|
|
62
|
+
expect(mocks.setReady).not.toHaveBeenCalled();
|
|
63
|
+
});
|
|
64
|
+
});
|