@redacto.io/consent-sdk-react 10.0.0-beta.1 → 10.0.0-beta.3

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.
Files changed (42) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/privacy-center.d.mts +2 -0
  3. package/dist/privacy-center.d.ts +2 -0
  4. package/dist/privacy-center.js +177 -49
  5. package/dist/privacy-center.mjs +177 -49
  6. package/package.json +1 -1
  7. package/src/RedactoPrivacyCenter/api/actions.ts +14 -1
  8. package/src/RedactoPrivacyCenter/components/AppShell/AppShell.tsx +28 -0
  9. package/src/RedactoPrivacyCenter/components/AppShell/UserMenu.tsx +25 -18
  10. package/src/RedactoPrivacyCenter/components/ConsentManager.tsx +7 -0
  11. package/src/RedactoPrivacyCenter/components/ConsentManagerCells.tsx +19 -2
  12. package/src/RedactoPrivacyCenter/components/ModifyConsentModal.tsx +1 -1
  13. package/src/RedactoPrivacyCenter/components/SelectUser.tsx +15 -1
  14. package/src/RedactoPrivacyCenter/lib/types.ts +9 -0
  15. package/src/RedactoPrivacyCenter/locales/as/translation.json +4 -1
  16. package/src/RedactoPrivacyCenter/locales/bho/translation.json +4 -1
  17. package/src/RedactoPrivacyCenter/locales/bn/translation.json +4 -1
  18. package/src/RedactoPrivacyCenter/locales/brx/translation.json +4 -1
  19. package/src/RedactoPrivacyCenter/locales/doi/translation.json +4 -1
  20. package/src/RedactoPrivacyCenter/locales/en/translation.json +4 -1
  21. package/src/RedactoPrivacyCenter/locales/gom/translation.json +4 -1
  22. package/src/RedactoPrivacyCenter/locales/gu/translation.json +4 -1
  23. package/src/RedactoPrivacyCenter/locales/hi/translation.json +4 -1
  24. package/src/RedactoPrivacyCenter/locales/kn/translation.json +4 -1
  25. package/src/RedactoPrivacyCenter/locales/ks/translation.json +4 -1
  26. package/src/RedactoPrivacyCenter/locales/mai/translation.json +4 -1
  27. package/src/RedactoPrivacyCenter/locales/ml/translation.json +4 -1
  28. package/src/RedactoPrivacyCenter/locales/mni-Mtei/translation.json +4 -1
  29. package/src/RedactoPrivacyCenter/locales/mr/translation.json +4 -1
  30. package/src/RedactoPrivacyCenter/locales/ne/translation.json +4 -1
  31. package/src/RedactoPrivacyCenter/locales/or/translation.json +4 -1
  32. package/src/RedactoPrivacyCenter/locales/pa/translation.json +4 -1
  33. package/src/RedactoPrivacyCenter/locales/sa/translation.json +4 -1
  34. package/src/RedactoPrivacyCenter/locales/sat/translation.json +4 -1
  35. package/src/RedactoPrivacyCenter/locales/sd/translation.json +4 -1
  36. package/src/RedactoPrivacyCenter/locales/ta/translation.json +4 -1
  37. package/src/RedactoPrivacyCenter/locales/te/translation.json +4 -1
  38. package/src/RedactoPrivacyCenter/locales/ur/translation.json +4 -1
  39. package/src/RedactoPrivacyCenter/styles/injectStyles.ts +11 -0
  40. package/tests/ManageConsentErrorMapping.test.tsx +85 -0
  41. package/tests/RevocablePurpose.test.tsx +146 -0
  42. package/tests/RevokeWarningMessage.test.tsx +151 -0
@@ -0,0 +1,146 @@
1
+ import { render, screen, fireEvent } from '@testing-library/react';
2
+ import { describe, it, expect, vi } from 'vitest';
3
+
4
+ // react-i18next is an optional peer dep that is not installed in this
5
+ // package's node_modules. Stub it with a minimal `useTranslation` that
6
+ // resolves the keys used by the components under test, honouring the inline
7
+ // English fallback passed as the 2nd argument (mirrors the real i18next
8
+ // behaviour when a key is missing).
9
+ vi.mock('react-i18next', () => {
10
+ const en: Record<string, string> = {
11
+ modifyButton: 'Modify',
12
+ modifyConsent: 'Modify Consent',
13
+ closeModal: 'Close',
14
+ cancel: 'Cancel',
15
+ revokeConsent: 'Revoke Consent',
16
+ revokeConsentWarning: 'If you revoke this consent, you may lose access.',
17
+ aboutToModifyConsent: 'You are about to modify this consent.',
18
+ noDataElementsAvailable: 'No data elements available',
19
+ purposeNotRevocable: "This purpose can't be withdrawn directly.",
20
+ noActionsAvailable: 'No actions available for this record.',
21
+ };
22
+ const interpolate = (template: string, vars?: Record<string, unknown>) =>
23
+ template.replace(/\{\{(\w+)\}\}/g, (_match, key: string) => {
24
+ const value = vars?.[key];
25
+ return value === undefined || value === null ? '' : String(value);
26
+ });
27
+ // Supports the `t(key, fallbackString)` and `t(key, opts)` call shapes used
28
+ // across the components. When the key is unknown, fall back to the inline
29
+ // English string if one was supplied, otherwise echo the key.
30
+ const t = (key: string, second?: unknown, opts?: Record<string, unknown>) => {
31
+ const fallback = typeof second === 'string' ? second : undefined;
32
+ const interpolationOpts =
33
+ typeof second === 'object' && second !== null
34
+ ? (second as Record<string, unknown>)
35
+ : opts;
36
+ const template = en[key] ?? fallback ?? key;
37
+ return interpolate(template, interpolationOpts);
38
+ };
39
+ return {
40
+ useTranslation: () => ({ t, i18n: { language: 'en' } }),
41
+ };
42
+ });
43
+
44
+ // Icon peer deps — stub the icons used by the modal.
45
+ vi.mock('lucide-react', () => ({
46
+ ChevronDownIcon: () => null,
47
+ X: () => null,
48
+ Smartphone: () => null,
49
+ LaptopMinimal: () => null,
50
+ }));
51
+
52
+ vi.mock('react-icons/pi', () => ({
53
+ PiWarningCircleBold: () => null,
54
+ }));
55
+
56
+ import { renderModifyButton } from '../src/RedactoPrivacyCenter/components/ConsentManagerCells';
57
+ import { ModifyConsentModal } from '../src/RedactoPrivacyCenter/components/ModifyConsentModal';
58
+ import { NavigationProvider } from '../src/RedactoPrivacyCenter/context/NavigationContext';
59
+ import { ConsentStatusEnum } from '../src/RedactoPrivacyCenter/lib/types';
60
+ import type { ConsentModalRecord } from '../src/RedactoPrivacyCenter/lib/types';
61
+
62
+ const makeRecord = (
63
+ overrides: Partial<ConsentModalRecord> = {},
64
+ ): ConsentModalRecord => ({
65
+ purpose_uuid: 'purpose-1',
66
+ name: 'Marketing analytics',
67
+ description: 'Improve product recommendations',
68
+ status: ConsentStatusEnum.ACTIVE,
69
+ given_date: '2026-01-01T00:00:00Z',
70
+ valid_till: null,
71
+ method: 'WEB',
72
+ data_elements: [],
73
+ link_reason: null,
74
+ product_uuid: 'prod-1',
75
+ product_name: 'Acme Loyalty',
76
+ product_description: null,
77
+ nominator: null,
78
+ ...overrides,
79
+ });
80
+
81
+ // A TFunction-compatible stub for the pure render helper (which receives `t`
82
+ // directly rather than via the react-i18next mock). Resolves the handful of
83
+ // keys it uses and honours the inline English fallback like real i18next.
84
+ const tStubKeys: Record<string, string> = {
85
+ modifyButton: 'Modify',
86
+ };
87
+ const tStub = ((key: string, second?: unknown) =>
88
+ tStubKeys[key] ??
89
+ (typeof second === 'string' ? second : key)) as unknown as Parameters<
90
+ typeof renderModifyButton
91
+ >[2];
92
+
93
+ describe('non-revocable purpose', () => {
94
+ it('renders the modify/withdraw control disabled for a non-revocable ACTIVE purpose', () => {
95
+ const record = makeRecord({ revocable: false });
96
+ render(<>{renderModifyButton(record, vi.fn(), tStub)}</>);
97
+
98
+ const button = screen.getByRole('button', { name: 'Modify' });
99
+ expect(button).toBeDisabled();
100
+ expect(button).toHaveAttribute(
101
+ 'title',
102
+ "This purpose can't be withdrawn directly.",
103
+ );
104
+ });
105
+
106
+ it('does not invoke onModify when the disabled non-revocable control is clicked', () => {
107
+ const onModify = vi.fn();
108
+ const record = makeRecord({ revocable: false });
109
+ render(<>{renderModifyButton(record, onModify, tStub)}</>);
110
+
111
+ fireEvent.click(screen.getByRole('button', { name: 'Modify' }));
112
+ expect(onModify).not.toHaveBeenCalled();
113
+ });
114
+ });
115
+
116
+ describe('revocable purpose (regression)', () => {
117
+ it('renders the modify control enabled when revocable is absent (older backends)', () => {
118
+ const record = makeRecord(); // no `revocable` field
119
+ const onModify = vi.fn();
120
+ render(<>{renderModifyButton(record, onModify, tStub)}</>);
121
+
122
+ const button = screen.getByRole('button', { name: 'Modify' });
123
+ expect(button).not.toBeDisabled();
124
+ fireEvent.click(button);
125
+ expect(onModify).toHaveBeenCalledTimes(1);
126
+ });
127
+
128
+ it('offers the revoke action in the modal for a revocable ACTIVE record', () => {
129
+ const record = makeRecord({ revocable: true });
130
+ render(
131
+ <NavigationProvider>
132
+ <ModifyConsentModal
133
+ isOpen
134
+ onClose={vi.fn()}
135
+ selectedRecord={record}
136
+ isLoading={false}
137
+ onManageConsent={vi.fn()}
138
+ />
139
+ </NavigationProvider>,
140
+ );
141
+
142
+ expect(
143
+ screen.getByRole('button', { name: 'Revoke Consent' }),
144
+ ).toBeInTheDocument();
145
+ });
146
+ });
@@ -0,0 +1,151 @@
1
+ import { render, screen } from '@testing-library/react';
2
+ import { describe, it, expect, vi } from 'vitest';
3
+
4
+ // react-i18next is an optional peer dep that is not installed in this
5
+ // package's node_modules. Stub it with a minimal `useTranslation` that
6
+ // resolves the keys used by the components under test, honouring the inline
7
+ // English fallback passed as the 2nd argument (mirrors the real i18next
8
+ // behaviour when a key is missing).
9
+ vi.mock('react-i18next', () => {
10
+ const en: Record<string, string> = {
11
+ modifyConsent: 'Modify Consent',
12
+ closeModal: 'Close',
13
+ cancel: 'Cancel',
14
+ revokeConsent: 'Revoke Consent',
15
+ revokeConsentWarning: 'If you revoke this consent, you may lose access.',
16
+ regrantConsent: 'Regrant Consent',
17
+ regrantConsentWarning: 'Regranting will restore this consent.',
18
+ aboutToModifyConsent: 'You are about to modify this consent.',
19
+ aboutToRegrantConsent: 'You are about to regrant this consent.',
20
+ noDataElementsAvailable: 'No data elements available',
21
+ noActionsAvailable: 'No actions available for this record.',
22
+ };
23
+ const interpolate = (template: string, vars?: Record<string, unknown>) =>
24
+ template.replace(/\{\{(\w+)\}\}/g, (_match, key: string) => {
25
+ const value = vars?.[key];
26
+ return value === undefined || value === null ? '' : String(value);
27
+ });
28
+ // Supports the `t(key, fallbackString)` and `t(key, opts)` call shapes used
29
+ // across the components. When the key is unknown, fall back to the inline
30
+ // English string if one was supplied, otherwise echo the key.
31
+ const t = (key: string, second?: unknown, opts?: Record<string, unknown>) => {
32
+ const fallback = typeof second === 'string' ? second : undefined;
33
+ const interpolationOpts =
34
+ typeof second === 'object' && second !== null
35
+ ? (second as Record<string, unknown>)
36
+ : opts;
37
+ const template = en[key] ?? fallback ?? key;
38
+ return interpolate(template, interpolationOpts);
39
+ };
40
+ return {
41
+ useTranslation: () => ({ t, i18n: { language: 'en' } }),
42
+ };
43
+ });
44
+
45
+ // Icon peer deps — stub the icons used by the modal.
46
+ vi.mock('lucide-react', () => ({
47
+ ChevronDownIcon: () => null,
48
+ X: () => null,
49
+ Smartphone: () => null,
50
+ LaptopMinimal: () => null,
51
+ }));
52
+
53
+ vi.mock('react-icons/pi', () => ({
54
+ PiWarningCircleBold: () => null,
55
+ }));
56
+
57
+ import { ModifyConsentModal } from '../src/RedactoPrivacyCenter/components/ModifyConsentModal';
58
+ import { NavigationProvider } from '../src/RedactoPrivacyCenter/context/NavigationContext';
59
+ import { ConsentStatusEnum } from '../src/RedactoPrivacyCenter/lib/types';
60
+ import type { ConsentModalRecord } from '../src/RedactoPrivacyCenter/lib/types';
61
+
62
+ const DEFAULT_WARNING = 'If you revoke this consent, you may lose access.';
63
+ const CUSTOM_WARNING =
64
+ 'Revoking marketing consent will stop all promotional emails immediately.';
65
+
66
+ const makeRecord = (
67
+ overrides: Partial<ConsentModalRecord> = {},
68
+ ): ConsentModalRecord => ({
69
+ purpose_uuid: 'purpose-1',
70
+ name: 'Marketing analytics',
71
+ description: 'Improve product recommendations',
72
+ status: ConsentStatusEnum.ACTIVE,
73
+ given_date: '2026-01-01T00:00:00Z',
74
+ valid_till: null,
75
+ method: 'WEB',
76
+ data_elements: [],
77
+ link_reason: null,
78
+ product_uuid: 'prod-1',
79
+ product_name: 'Acme Loyalty',
80
+ product_description: null,
81
+ nominator: null,
82
+ ...overrides,
83
+ });
84
+
85
+ const renderModal = (record: ConsentModalRecord) =>
86
+ render(
87
+ <NavigationProvider>
88
+ <ModifyConsentModal
89
+ isOpen
90
+ onClose={vi.fn()}
91
+ selectedRecord={record}
92
+ isLoading={false}
93
+ onManageConsent={vi.fn()}
94
+ />
95
+ </NavigationProvider>,
96
+ );
97
+
98
+ describe('per-purpose revoke warning message', () => {
99
+ it('shows the custom warning when revoke_warning_message is set on a revoke record', () => {
100
+ renderModal(makeRecord({ revoke_warning_message: CUSTOM_WARNING }));
101
+
102
+ expect(screen.getByText(CUSTOM_WARNING)).toBeInTheDocument();
103
+ // The localized default must be suppressed when a custom message exists.
104
+ expect(screen.queryByText(DEFAULT_WARNING)).not.toBeInTheDocument();
105
+ // Button label is untouched — still the default revoke copy.
106
+ expect(
107
+ screen.getByRole('button', { name: 'Revoke Consent' }),
108
+ ).toBeInTheDocument();
109
+ });
110
+
111
+ it('shows the localized default warning when revoke_warning_message is null', () => {
112
+ renderModal(makeRecord({ revoke_warning_message: null }));
113
+
114
+ expect(screen.getByText(DEFAULT_WARNING)).toBeInTheDocument();
115
+ expect(
116
+ screen.getByRole('button', { name: 'Revoke Consent' }),
117
+ ).toBeInTheDocument();
118
+ });
119
+
120
+ it('shows the localized default warning when revoke_warning_message is an empty string', () => {
121
+ renderModal(makeRecord({ revoke_warning_message: '' }));
122
+
123
+ expect(screen.getByText(DEFAULT_WARNING)).toBeInTheDocument();
124
+ });
125
+
126
+ it('shows the localized default warning when revoke_warning_message is absent (older backends)', () => {
127
+ renderModal(makeRecord()); // no `revoke_warning_message` field
128
+
129
+ expect(screen.getByText(DEFAULT_WARNING)).toBeInTheDocument();
130
+ expect(
131
+ screen.getByRole('button', { name: 'Revoke Consent' }),
132
+ ).toBeInTheDocument();
133
+ });
134
+
135
+ it('does not apply the custom revoke warning to the regrant branch', () => {
136
+ // A withdrawn record uses the regrant action; the per-purpose revoke
137
+ // message must never leak into the regrant warning.
138
+ renderModal(
139
+ makeRecord({
140
+ status: ConsentStatusEnum.WITHDRAWN,
141
+ revoke_warning_message: CUSTOM_WARNING,
142
+ }),
143
+ );
144
+
145
+ expect(screen.getByText('Regranting will restore this consent.')).toBeInTheDocument();
146
+ expect(screen.queryByText(CUSTOM_WARNING)).not.toBeInTheDocument();
147
+ expect(
148
+ screen.getByRole('button', { name: 'Regrant Consent' }),
149
+ ).toBeInTheDocument();
150
+ });
151
+ });