Skip to content

Commit 4996e62

Browse files
committed
lint
1 parent aefb3ce commit 4996e62

4 files changed

Lines changed: 93 additions & 38 deletions

File tree

source/react-native/b2b/src/__tests__/providers.test.tsx

Lines changed: 71 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ import {
1313
import {
1414
ApiB2bSessionV1MemberSession,
1515
ApiOrganizationV1Member,
16+
ApiOrganizationV1Organization,
1617
B2BAuthenticationState,
1718
StytchB2B,
1819
B2BSessionsAuthenticateParameters,
19-
B2BSessionsAuthenticateResponse
20+
B2BSessionsAuthenticateResponse,
2021
} from '../../lib/b2b-headless.mjs';
2122

2223
// ---------------------------------------------------------------------------
@@ -30,8 +31,12 @@ type AnyState =
3031
type ObserverCallback = (state: AnyState) => void | Promise<void>;
3132

3233
interface MockStytchB2BClient {
33-
authenticationStateObserver: jest.Mock<any>;
34-
session: { authenticate: jest.Mock<(params: B2BSessionsAuthenticateParameters) => Promise<B2BSessionsAuthenticateResponse>> };
34+
authenticationStateObserver: jest.Mock<(params: ObserverCallback) => AnyState>;
35+
session: {
36+
authenticate: jest.Mock<
37+
(params: B2BSessionsAuthenticateParameters) => Promise<B2BSessionsAuthenticateResponse>
38+
>;
39+
};
3540
}
3641

3742
/**
@@ -54,7 +59,9 @@ function makeStytchMock() {
5459
return { stop };
5560
}),
5661
session: {
57-
authenticate: jest.fn<() => Promise<B2BSessionsAuthenticateResponse>>().mockResolvedValue({} as B2BSessionsAuthenticateResponse),
62+
authenticate: jest
63+
.fn<() => Promise<B2BSessionsAuthenticateResponse>>()
64+
.mockResolvedValue({} as B2BSessionsAuthenticateResponse),
5865
},
5966
};
6067

@@ -74,12 +81,12 @@ function makeStytchMock() {
7481

7582
const mockMember = {} as unknown as ApiOrganizationV1Member;
7683
const mockMemberSession = {} as unknown as ApiB2bSessionV1MemberSession;
77-
84+
const mockOrganization = {} as unknown as ApiOrganizationV1Organization;
7885
function makeAuthenticatedState(
7986
member: ApiOrganizationV1Member = mockMember,
8087
memberSession: ApiB2bSessionV1MemberSession = mockMemberSession,
8188
) {
82-
return new B2BAuthenticationState.Authenticated(member, memberSession, null as any, '', '');
89+
return new B2BAuthenticationState.Authenticated(member, memberSession, mockOrganization, '', '');
8390
}
8491

8592
// ---------------------------------------------------------------------------
@@ -105,20 +112,32 @@ beforeEach(() => {
105112
describe('StytchB2BProvider', () => {
106113
it('subscribes to authenticationStateObserver on mount', () => {
107114
const { mockClient } = makeStytchMock();
108-
render(<StytchB2BProvider stytch={mockClient}><Text /></StytchB2BProvider>);
115+
render(
116+
<StytchB2BProvider stytch={mockClient}>
117+
<Text />
118+
</StytchB2BProvider>,
119+
);
109120
expect(mockClient.authenticationStateObserver).toHaveBeenCalled();
110121
});
111122

112123
it('stops the observer subscription on unmount', () => {
113124
const { mockClient, observerStops } = makeStytchMock();
114-
const { unmount } = render(<StytchB2BProvider stytch={mockClient}><Text /></StytchB2BProvider>);
125+
const { unmount } = render(
126+
<StytchB2BProvider stytch={mockClient}>
127+
<Text />
128+
</StytchB2BProvider>,
129+
);
115130
unmount();
116-
expect(observerStops.every(stop => stop.mock.calls.length > 0)).toBe(true);
131+
expect(observerStops.every((stop) => stop.mock.calls.length > 0)).toBe(true);
117132
});
118133

119134
it('removes the AppState listener on unmount', () => {
120135
const { mockClient } = makeStytchMock();
121-
const { unmount } = render(<StytchB2BProvider stytch={mockClient}><Text /></StytchB2BProvider>);
136+
const { unmount } = render(
137+
<StytchB2BProvider stytch={mockClient}>
138+
<Text />
139+
</StytchB2BProvider>,
140+
);
122141
unmount();
123142
expect(appStateRemove).toHaveBeenCalled();
124143
});
@@ -128,7 +147,11 @@ describe('StytchB2BProvider', () => {
128147

129148
const { result } = renderHook(
130149
() => ({ member: useStytchMember(), memberSession: useStytchMemberSession() }),
131-
{ wrapper: ({ children }) => <StytchB2BProvider stytch={mockClient}>{children}</StytchB2BProvider> },
150+
{
151+
wrapper: ({ children }) => (
152+
<StytchB2BProvider stytch={mockClient}>{children}</StytchB2BProvider>
153+
),
154+
},
132155
);
133156

134157
await emitState(makeAuthenticatedState(mockMember, mockMemberSession));
@@ -142,7 +165,11 @@ describe('StytchB2BProvider', () => {
142165

143166
const { result } = renderHook(
144167
() => ({ member: useStytchMember(), memberSession: useStytchMemberSession() }),
145-
{ wrapper: ({ children }) => <StytchB2BProvider stytch={mockClient}>{children}</StytchB2BProvider> },
168+
{
169+
wrapper: ({ children }) => (
170+
<StytchB2BProvider stytch={mockClient}>{children}</StytchB2BProvider>
171+
),
172+
},
146173
);
147174

148175
await emitState(makeAuthenticatedState());
@@ -157,10 +184,11 @@ describe('StytchB2BProvider', () => {
157184
const { mockClient, emitState } = makeStytchMock();
158185
const authenticatedState = makeAuthenticatedState();
159186

160-
const { result } = renderHook(
161-
() => useStytchB2BAuthenticationState(),
162-
{ wrapper: ({ children }) => <StytchB2BProvider stytch={mockClient}>{children}</StytchB2BProvider> },
163-
);
187+
const { result } = renderHook(() => useStytchB2BAuthenticationState(), {
188+
wrapper: ({ children }) => (
189+
<StytchB2BProvider stytch={mockClient}>{children}</StytchB2BProvider>
190+
),
191+
});
164192

165193
expect(result.current).toBeInstanceOf(B2BAuthenticationState.Loading);
166194
await emitState(authenticatedState);
@@ -170,7 +198,11 @@ describe('StytchB2BProvider', () => {
170198
it('calls session.authenticate when app becomes active and session is Authenticated', async () => {
171199
const { mockClient, emitState } = makeStytchMock();
172200

173-
render(<StytchB2BProvider stytch={mockClient}><Text /></StytchB2BProvider>);
201+
render(
202+
<StytchB2BProvider stytch={mockClient}>
203+
<Text />
204+
</StytchB2BProvider>,
205+
);
174206

175207
await act(async () => {
176208
capturedAppStateHandler!('active');
@@ -185,7 +217,11 @@ describe('StytchB2BProvider', () => {
185217
const { mockClient, emitState } = makeStytchMock();
186218
mockClient.session.authenticate.mockRejectedValueOnce(new Error('network error'));
187219

188-
render(<StytchB2BProvider stytch={mockClient}><Text /></StytchB2BProvider>);
220+
render(
221+
<StytchB2BProvider stytch={mockClient}>
222+
<Text />
223+
</StytchB2BProvider>,
224+
);
189225

190226
await act(async () => {
191227
capturedAppStateHandler!('active');
@@ -198,7 +234,11 @@ describe('StytchB2BProvider', () => {
198234
it('does not call session.authenticate when app becomes active but state is not Authenticated', async () => {
199235
const { mockClient, emitState } = makeStytchMock();
200236

201-
render(<StytchB2BProvider stytch={mockClient}><Text /></StytchB2BProvider>);
237+
render(
238+
<StytchB2BProvider stytch={mockClient}>
239+
<Text />
240+
</StytchB2BProvider>,
241+
);
202242

203243
await act(async () => {
204244
capturedAppStateHandler!('active');
@@ -222,7 +262,9 @@ describe('withStytchB2B', () => {
222262
it('makes the stytch client available via useStytchB2B inside the provider', () => {
223263
const { mockClient } = makeStytchMock();
224264
const { result } = renderHook(() => useStytchB2B(), {
225-
wrapper: ({ children }) => <StytchB2BProvider stytch={mockClient}>{children}</StytchB2BProvider>,
265+
wrapper: ({ children }) => (
266+
<StytchB2BProvider stytch={mockClient}>{children}</StytchB2BProvider>
267+
),
226268
});
227269
expect(result.current).toBe(mockClient);
228270
});
@@ -232,15 +274,19 @@ describe('withStytchMember', () => {
232274
it('returns undefined when no member is in context', () => {
233275
const { mockClient } = makeStytchMock();
234276
const { result } = renderHook(() => useStytchMember(), {
235-
wrapper: ({ children }) => <StytchB2BProvider stytch={mockClient}>{children}</StytchB2BProvider>,
277+
wrapper: ({ children }) => (
278+
<StytchB2BProvider stytch={mockClient}>{children}</StytchB2BProvider>
279+
),
236280
});
237281
expect(result.current).toBeUndefined();
238282
});
239283

240284
it('reflects the member once Authenticated state fires', async () => {
241285
const { mockClient, emitState } = makeStytchMock();
242286
const { result } = renderHook(() => useStytchMember(), {
243-
wrapper: ({ children }) => <StytchB2BProvider stytch={mockClient}>{children}</StytchB2BProvider>,
287+
wrapper: ({ children }) => (
288+
<StytchB2BProvider stytch={mockClient}>{children}</StytchB2BProvider>
289+
),
244290
});
245291
await emitState(makeAuthenticatedState(mockMember));
246292
expect(result.current).toBe(mockMember);
@@ -251,7 +297,9 @@ describe('withStytchMemberSession', () => {
251297
it('returns undefined when no session is in context', () => {
252298
const { mockClient } = makeStytchMock();
253299
const { result } = renderHook(() => useStytchMemberSession(), {
254-
wrapper: ({ children }) => <StytchB2BProvider stytch={mockClient}>{children}</StytchB2BProvider>,
300+
wrapper: ({ children }) => (
301+
<StytchB2BProvider stytch={mockClient}>{children}</StytchB2BProvider>
302+
),
255303
});
256304
expect(result.current).toBeUndefined();
257305
});

source/react-native/consumer/src/__tests__/providers.test.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ type AnyState =
2525
type ObserverCallback = (state: AnyState) => void | Promise<void>;
2626

2727
interface MockStytchClient {
28-
authenticationStateObserver: jest.Mock<any>;
29-
session: { authenticate: jest.Mock<(params: SessionsAuthenticateParameters) => Promise<SessionsAuthenticateResponse>> };
28+
authenticationStateObserver: jest.Mock<(params: ObserverCallback) => AnyState>;
29+
session: {
30+
authenticate: jest.Mock<
31+
(params: SessionsAuthenticateParameters) => Promise<SessionsAuthenticateResponse>
32+
>;
33+
};
3034
}
3135

3236
/**

source/react-native/consumer/src/providers.tsx

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,21 @@ export const StytchProvider = ({ stytch, children }: StytchProviderProps): React
100100
}, [stytch]);
101101

102102
useEffect(() => {
103-
const observationJob = stytch.authenticationStateObserver((state: ConsumerAuthenticationState) => {
104-
let newUser: ApiUserV1User | undefined = undefined;
105-
let newSession: ApiSessionV1Session | undefined = undefined;
106-
if (state instanceof ConsumerAuthenticationState.Authenticated) {
107-
newUser = state.user;
108-
newSession = state.session;
109-
}
110-
setClientState((oldState) => {
111-
const newState = { user: newUser, session: newSession };
112-
return mergeWithStableProps(oldState, newState);
113-
});
114-
setAuthenticationState(state);
115-
});
103+
const observationJob = stytch.authenticationStateObserver(
104+
(state: ConsumerAuthenticationState) => {
105+
let newUser: ApiUserV1User | undefined = undefined;
106+
let newSession: ApiSessionV1Session | undefined = undefined;
107+
if (state instanceof ConsumerAuthenticationState.Authenticated) {
108+
newUser = state.user;
109+
newSession = state.session;
110+
}
111+
setClientState((oldState) => {
112+
const newState = { user: newUser, session: newSession };
113+
return mergeWithStableProps(oldState, newState);
114+
});
115+
setAuthenticationState(state);
116+
},
117+
);
116118
return () => {
117119
observationJob.stop();
118120
};

source/react-native/shared/src/__mocks__/b2b-headless.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ export const B2BAuthenticationState = {
1313
};
1414
export class B2BSessionsAuthenticateParameters {}
1515
export class B2BSessionsAuthenticateResponse {}
16+
export class ApiOrganizationV1Organization {}

0 commit comments

Comments
 (0)