@micha.bigler/ui-core-micha 2.8.1 → 2.8.2

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.
@@ -61,7 +61,11 @@ function getInitialPushState() {
61
61
  && 'PushManager' in window;
62
62
  return supported ? null : { supported: false, subscribed: false };
63
63
  }
64
- export function OnboardingProvider({ children, extraSteps = [] }) {
64
+ // Stable empty-object default an inline `{}` default would create a new
65
+ // reference every render, invalidating the `ctx` memo below for every
66
+ // consumer even when no extraContext is passed.
67
+ const EMPTY_EXTRA_CONTEXT = {};
68
+ export function OnboardingProvider({ children, extraSteps = [], extraContext = EMPTY_EXTRA_CONTEXT }) {
65
69
  const { user } = useContext(AuthContext);
66
70
  const [configMap, setConfigMap] = useState({});
67
71
  const [configLoaded, setConfigLoaded] = useState(false);
@@ -146,7 +150,8 @@ export function OnboardingProvider({ children, extraSteps = [] }) {
146
150
  return next;
147
151
  });
148
152
  }, []);
149
- const ctx = useMemo(() => ({ user, pushState, emailOptedIn }), [user, pushState, emailOptedIn]);
153
+ // extraContext is spread last: duplicate core keys intentionally override core values, so apps should use distinct names.
154
+ const ctx = useMemo(() => (Object.assign({ user, pushState, emailOptedIn }, extraContext)), [user, pushState, emailOptedIn, extraContext]);
150
155
  const activeSteps = useMemo(() => {
151
156
  if (!configLoaded || !user)
152
157
  return [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@micha.bigler/ui-core-micha",
3
- "version": "2.8.1",
3
+ "version": "2.8.2",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "repository": {
@@ -70,7 +70,12 @@ function getInitialPushState() {
70
70
  return supported ? null : { supported: false, subscribed: false };
71
71
  }
72
72
 
73
- export function OnboardingProvider({ children, extraSteps = [] }) {
73
+ // Stable empty-object default an inline `{}` default would create a new
74
+ // reference every render, invalidating the `ctx` memo below for every
75
+ // consumer even when no extraContext is passed.
76
+ const EMPTY_EXTRA_CONTEXT = {};
77
+
78
+ export function OnboardingProvider({ children, extraSteps = [], extraContext = EMPTY_EXTRA_CONTEXT }) {
74
79
  const { user } = useContext(AuthContext);
75
80
  const [configMap, setConfigMap] = useState({});
76
81
  const [configLoaded, setConfigLoaded] = useState(false);
@@ -162,7 +167,11 @@ export function OnboardingProvider({ children, extraSteps = [] }) {
162
167
  });
163
168
  }, []);
164
169
 
165
- const ctx = useMemo(() => ({ user, pushState, emailOptedIn }), [user, pushState, emailOptedIn]);
170
+ // extraContext is spread last: duplicate core keys intentionally override core values, so apps should use distinct names.
171
+ const ctx = useMemo(
172
+ () => ({ user, pushState, emailOptedIn, ...extraContext }),
173
+ [user, pushState, emailOptedIn, extraContext],
174
+ );
166
175
  const activeSteps = useMemo(() => {
167
176
  if (!configLoaded || !user) return [];
168
177
  return selectActiveSteps(descriptors, configMap, ctx, dismissedSet);
@@ -21,10 +21,22 @@ function Probe() {
21
21
  return <span data-testid="email-opted-in">{String(onboarding?.ctx.emailOptedIn)}</span>;
22
22
  }
23
23
 
24
- function renderProvider() {
24
+ function ActiveStepsProbe() {
25
+ const onboarding = useOnboarding();
26
+ return <span data-testid="active-steps">{onboarding?.activeSteps.map((step) => step.id).join(',')}</span>;
27
+ }
28
+
29
+ function renderProvider({ extraContext, extraSteps, children = <Probe /> } = {}) {
25
30
  return render(
26
- <AuthContext.Provider value={{ user: { id: 1 } }}>
27
- <OnboardingProvider><Probe /></OnboardingProvider>
31
+ <AuthContext.Provider value={{
32
+ user: {
33
+ id: 1,
34
+ accepted_privacy_statement: true,
35
+ first_name: 'Ada',
36
+ last_name: 'Lovelace',
37
+ },
38
+ }}>
39
+ <OnboardingProvider extraContext={extraContext} extraSteps={extraSteps}>{children}</OnboardingProvider>
28
40
  </AuthContext.Provider>,
29
41
  );
30
42
  }
@@ -54,3 +66,43 @@ describe('OnboardingProvider notification preferences', () => {
54
66
  expect(screen.getByTestId('email-opted-in').textContent).toBe('false');
55
67
  });
56
68
  });
69
+
70
+ describe('OnboardingProvider extra context', () => {
71
+ const unreadMessagesCondition = vi.fn((ctx) => (ctx.unreadCount || 0) > 0);
72
+ const unreadMessagesStep = {
73
+ id: 'unread_messages',
74
+ condition: unreadMessagesCondition,
75
+ persistDismissed: false,
76
+ };
77
+
78
+ beforeEach(() => {
79
+ cleanup();
80
+ vi.clearAllMocks();
81
+ onboardingApi.getOnboardingStepConfig.mockResolvedValue([]);
82
+ notificationsApi.getNotificationPreferences.mockResolvedValue({ email_opt_in: true });
83
+ });
84
+
85
+ it('shows an app step when its extra context condition is met', async () => {
86
+ renderProvider({
87
+ extraContext: { unreadCount: 2 },
88
+ extraSteps: [unreadMessagesStep],
89
+ children: <ActiveStepsProbe />,
90
+ });
91
+
92
+ await waitFor(() => expect(screen.getByTestId('active-steps').textContent).toContain('unread_messages'));
93
+ });
94
+
95
+ it.each([
96
+ ['zero', { unreadCount: 0 }],
97
+ ['omitted', undefined],
98
+ ])('does not show an app step when unreadCount is %s', async (_label, extraContext) => {
99
+ renderProvider({
100
+ extraContext,
101
+ extraSteps: [unreadMessagesStep],
102
+ children: <ActiveStepsProbe />,
103
+ });
104
+
105
+ await waitFor(() => expect(unreadMessagesCondition).toHaveBeenCalled());
106
+ expect(screen.getByTestId('active-steps').textContent).not.toContain('unread_messages');
107
+ });
108
+ });