@cccsaurora/howler-ui 3.1.0-dev.1618 → 3.1.0-dev.1619

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
@@ -97,7 +97,7 @@
97
97
  "url": "https://github.com/CybercentreCanada/howler"
98
98
  },
99
99
  "type": "module",
100
- "version": "3.1.0-dev.1618",
100
+ "version": "3.1.0-dev.1619",
101
101
  "exports": {
102
102
  "./i18n": "./i18n.js",
103
103
  "./index.css": "./index.css",
@@ -1,3 +1,15 @@
1
1
  import { type PropsWithChildren } from 'react';
2
- declare const Provider: React.FC<PropsWithChildren<{}>>;
2
+ export interface CluePluginOptions {
3
+ baseURL?: string;
4
+ getToken?: () => string;
5
+ publicIconify?: boolean;
6
+ customIconify?: string;
7
+ includeContext?: boolean;
8
+ defaultTimeout?: number;
9
+ chunkSize?: number;
10
+ }
11
+ type ProviderProps = PropsWithChildren<{
12
+ options?: CluePluginOptions;
13
+ }>;
14
+ declare const Provider: React.FC<ProviderProps>;
3
15
  export default Provider;
@@ -1,13 +1,34 @@
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
- return (_jsx(ClueProvider, { baseURL: location.origin + '/api/v1/clue', getToken: () => getStored(StorageKey.APP_TOKEN) ?? '', enabled: features.clue, publicIconify: location.origin.includes('localhost'), customIconify: location.origin.replace(/howler(-stg)?/, 'icons'), defaultTimeout: 5, i18next: useTranslation('clue'), chunkSize: 50, children: children }));
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({ storageType: 'memory', replicate: true, baseURL, getToken }).then(database => {
24
+ if (!cancelled) {
25
+ setDatabase(database);
26
+ }
27
+ });
28
+ return () => {
29
+ cancelled = true;
30
+ };
31
+ }, [baseURL, features.clue, getToken]);
32
+ 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
33
  };
13
34
  export default Provider;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,105 @@
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 replicated 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
+ });
74
+ await waitFor(() => {
75
+ expect(mocks.buildDatabase).toHaveBeenCalledWith({
76
+ storageType: 'memory',
77
+ replicate: true,
78
+ baseURL: 'https://howler.example/api/v1/clue',
79
+ getToken
80
+ });
81
+ });
82
+ expect(getProviderProps()).toMatchObject({
83
+ baseURL: 'https://howler.example/api/v1/clue',
84
+ database,
85
+ enabled: true,
86
+ publicIconify: false,
87
+ customIconify: 'https://icons.example',
88
+ includeContext: true,
89
+ defaultTimeout: 7,
90
+ chunkSize: 25
91
+ });
92
+ });
93
+ it('ignores a database that resolves after the provider unmounts', async () => {
94
+ let resolveDatabase = () => { };
95
+ mocks.buildDatabase.mockReturnValue(new Promise(resolve => {
96
+ resolveDatabase = resolve;
97
+ }));
98
+ const { unmount } = renderProvider({ configuration: { features: { clue: true } } });
99
+ unmount();
100
+ await act(async () => {
101
+ resolveDatabase({ name: 'late' });
102
+ });
103
+ expect(getProviderProps().database).toBeUndefined();
104
+ });
105
+ });
@@ -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(): import("react").FC<{
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[];
@@ -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
- return Provider;
49
+ const options = this.options;
50
+ return ({ children }) => _jsx(Provider, { options: options, children: children });
45
51
  }
46
52
  setup() {
47
53
  return useSetup;
@@ -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.configuration?.features ?? {};
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.borealis || features.clue) {
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
+ });