Skip to content

Commit

Permalink
Add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
poovamraj committed Nov 21, 2023
1 parent f587412 commit ce73ef0
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions src/webauth/__tests__/agent.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,21 +203,54 @@ describe('Agent', () => {
});

describe('handle app linking for SFSafariViewController', () => {
it('for login, with only SFSafariViewController AppLinking should be enabled', async () => {
it('with useSFSafariViewController AppLinking should be enabled', async () => {
await agent.login({}, { useSFSafariViewController: true });
expect(Linking.addEventListener).toHaveBeenCalledTimes(1);
});

it('for logout, with only SFSafariViewController AppLinking should be enabled', async () => {
it('without useSFSafariViewController AppLinking should be enabled', async () => {
await agent.login({}, {});
expect(Linking.addEventListener).toHaveBeenCalledTimes(0);
});

it('for logout, for only iOS platform AppLinking should be enabled', async () => {
it('for only iOS platform AppLinking should be enabled', async () => {
Platform.OS = 'android';
await agent.login({}, { useSFSafariViewController: true });
expect(Linking.addEventListener).toHaveBeenCalledTimes(0);
Platform.OS = 'ios'; //reset value to ios
});

it('when login crashes and AppLinking is enabled, listener for AppLinking should be removed', async () => {
let mockSubscription = {
remove: () => {},
};
jest.spyOn(mockSubscription, 'remove').mockReturnValueOnce({});
jest
.spyOn(Linking, 'addEventListener')
.mockReturnValueOnce(mockSubscription);
jest
.spyOn(nativeUtils, '_ensureNativeModuleIsInitialized')
.mockImplementationOnce(() => {
throw Error('123123');
});
try {
await agent.login({}, { useSFSafariViewController: true });
} catch (e) {}
expect(Linking.addEventListener).toHaveBeenCalledTimes(1);
expect(mockSubscription.remove).toHaveBeenCalledTimes(1);
});

it('when login crashes and AppLinking is not enabled, listener for AppLinking remove should not be called', async () => {
let mockSubscription = {
remove: () => {},
};
jest.spyOn(mockSubscription, 'remove').mockReturnValueOnce({});
jest
.spyOn(Linking, 'addEventListener')
.mockReturnValueOnce(mockSubscription);
await agent.login({}, { useSFSafariViewController: true });
expect(Linking.addEventListener).toHaveBeenCalledTimes(1);
expect(mockSubscription.remove).toHaveBeenCalledTimes(0);
});
});
});

0 comments on commit ce73ef0

Please sign in to comment.