Skip to content
This repository was archived by the owner on Mar 18, 2025. It is now read-only.

Commit 3d35265

Browse files
committed
add testing for components
1 parent ddf6753 commit 3d35265

File tree

4 files changed

+374
-4
lines changed

4 files changed

+374
-4
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import React from 'react';
2+
import { Text } from 'react-native';
3+
import type { CryptrConfig } from '../../utils/interfaces';
4+
import CryptrProvider from '../../models/CryptrProvider';
5+
import renderer from 'react-test-renderer';
6+
import CryptrLogOutButton from '../../components/CryptrLogOutButton';
7+
import { Locale } from '../../utils/enums';
8+
import { fireEvent, render } from '@testing-library/react-native';
9+
import Cryptr from '../../models/Cryptr';
10+
interface JsonTree {
11+
children: any;
12+
}
13+
14+
jest.mock('../../models/Cryptr', () => ({
15+
startSecuredView: jest.fn(),
16+
getRefresh: jest.fn(),
17+
setRefresh: jest.fn(),
18+
}));
19+
20+
describe('CryptrLogOutButton', () => {
21+
const config: CryptrConfig = {
22+
tenant_domain: 'shark_academy',
23+
client_id: '123',
24+
audience: 'cryptr://audience',
25+
default_redirect_uri: 'cryptr://redirect-uri',
26+
};
27+
28+
it('should mount and allow cryptr logout button', () => {
29+
const tree = renderer.create(
30+
<CryptrProvider {...config}>
31+
<CryptrLogOutButton />
32+
</CryptrProvider>
33+
);
34+
expect(tree).not.toBeUndefined();
35+
const jsonTree = tree.toJSON() as JsonTree;
36+
expect(jsonTree.children).toBeNull();
37+
});
38+
39+
it('should display button when autoHide false', () => {
40+
const tree = renderer.create(
41+
<CryptrProvider {...config}>
42+
<CryptrLogOutButton autoHide={false} />
43+
</CryptrProvider>
44+
);
45+
expect(tree).not.toBeUndefined();
46+
const jsonTree = tree.toJSON() as JsonTree;
47+
expect(jsonTree.children[0].children).toEqual(['Log out']);
48+
expect(tree.root.findByType(CryptrLogOutButton).props).toEqual({
49+
autoHide: false,
50+
});
51+
});
52+
53+
it('should display french button when autoHide false', () => {
54+
const tree = renderer.create(
55+
<CryptrProvider {...config} default_locale={Locale.FR}>
56+
<CryptrLogOutButton autoHide={false} />
57+
</CryptrProvider>
58+
);
59+
expect(tree).not.toBeUndefined();
60+
const jsonTree = tree.toJSON() as JsonTree;
61+
expect(jsonTree.children[0].children).toEqual(['Déconnexion']);
62+
expect(tree.root.findByType(CryptrLogOutButton).props).toEqual({
63+
autoHide: false,
64+
});
65+
});
66+
67+
it('should display custom button when autoHide false', () => {
68+
const tree = renderer.create(
69+
<CryptrProvider {...config} default_locale={Locale.FR}>
70+
<CryptrLogOutButton text="Remove session" autoHide={false} />
71+
</CryptrProvider>
72+
);
73+
expect(tree).not.toBeUndefined();
74+
const jsonTree = tree.toJSON() as JsonTree;
75+
expect(jsonTree.children[0].children).toEqual(['Remove session']);
76+
expect(tree.root.findByType(CryptrLogOutButton).props).toEqual({
77+
autoHide: false,
78+
text: 'Remove session',
79+
});
80+
});
81+
82+
it('mount logout button with custom children', () => {
83+
const tree = renderer.create(
84+
<CryptrProvider {...config}>
85+
<CryptrLogOutButton autoHide={false}>
86+
<Text>Custom logout content</Text>
87+
</CryptrLogOutButton>
88+
</CryptrProvider>
89+
);
90+
expect(tree).not.toBeUndefined();
91+
const jsonTree = tree.toJSON() as JsonTree;
92+
expect(jsonTree.children[0].children).toEqual(['Custom logout content']);
93+
});
94+
95+
it('should start sso process on press action', () => {
96+
const { getByText } = render(
97+
<CryptrProvider {...config}>
98+
<CryptrLogOutButton autoHide={false}>
99+
<Text>Custom idp content</Text>
100+
</CryptrLogOutButton>
101+
</CryptrProvider>
102+
);
103+
const item = getByText('Custom idp content');
104+
const getRefreshFn = jest.spyOn(Cryptr, 'getRefresh');
105+
fireEvent.press(item);
106+
expect(getRefreshFn).toHaveBeenCalled();
107+
});
108+
});
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import React from 'react';
2+
import { Text } from 'react-native';
3+
import type { CryptrConfig } from '../../utils/interfaces';
4+
import CryptrProvider from '../../models/CryptrProvider';
5+
import renderer from 'react-test-renderer';
6+
import CryptrRefreshButton from '../../components/CryptrRefreshButton';
7+
import { Locale } from '../../utils/enums';
8+
import Cryptr from '../../models/Cryptr';
9+
import { fireEvent, render } from '@testing-library/react-native';
10+
interface JsonTree {
11+
children: any;
12+
}
13+
14+
jest.mock('../../models/Cryptr', () => ({
15+
startSecuredView: jest.fn(),
16+
getRefresh: jest.fn(),
17+
setRefresh: jest.fn(),
18+
}));
19+
20+
describe('CryptrRefreshButton', () => {
21+
const config: CryptrConfig = {
22+
tenant_domain: 'shark_academy',
23+
client_id: '123',
24+
audience: 'cryptr://audience',
25+
default_redirect_uri: 'cryptr://redirect-uri',
26+
};
27+
28+
it('should mount and allow cryptr logout button', () => {
29+
const tree = renderer.create(
30+
<CryptrProvider {...config}>
31+
<CryptrRefreshButton />
32+
</CryptrProvider>
33+
);
34+
expect(tree).not.toBeUndefined();
35+
const jsonTree = tree.toJSON() as JsonTree;
36+
expect(jsonTree.children).toBeNull();
37+
});
38+
39+
it('should display button when autoHide false', () => {
40+
const tree = renderer.create(
41+
<CryptrProvider {...config}>
42+
<CryptrRefreshButton autoHide={false} />
43+
</CryptrProvider>
44+
);
45+
expect(tree).not.toBeUndefined();
46+
const jsonTree = tree.toJSON() as JsonTree;
47+
expect(jsonTree.children[0].children).toEqual(['Refresh']);
48+
expect(tree.root.findByType(CryptrRefreshButton).props).toEqual({
49+
autoHide: false,
50+
});
51+
});
52+
53+
it('should display french button when autoHide false', () => {
54+
const tree = renderer.create(
55+
<CryptrProvider {...config} default_locale={Locale.FR}>
56+
<CryptrRefreshButton autoHide={false} />
57+
</CryptrProvider>
58+
);
59+
expect(tree).not.toBeUndefined();
60+
const jsonTree = tree.toJSON() as JsonTree;
61+
expect(jsonTree.children[0].children).toEqual(['Rafraîchir']);
62+
expect(tree.root.findByType(CryptrRefreshButton).props).toEqual({
63+
autoHide: false,
64+
});
65+
});
66+
67+
it('should display custom button when autoHide false', () => {
68+
const tree = renderer.create(
69+
<CryptrProvider {...config} default_locale={Locale.FR}>
70+
<CryptrRefreshButton text="Remove session" autoHide={false} />
71+
</CryptrProvider>
72+
);
73+
expect(tree).not.toBeUndefined();
74+
const jsonTree = tree.toJSON() as JsonTree;
75+
expect(jsonTree.children[0].children).toEqual(['Remove session']);
76+
expect(tree.root.findByType(CryptrRefreshButton).props).toEqual({
77+
autoHide: false,
78+
text: 'Remove session',
79+
});
80+
});
81+
82+
it('mount button with custom children', () => {
83+
const tree = renderer.create(
84+
<CryptrProvider {...config}>
85+
<CryptrRefreshButton autoHide={false}>
86+
<Text>Custom refersh content</Text>
87+
</CryptrRefreshButton>
88+
</CryptrProvider>
89+
);
90+
expect(tree).not.toBeUndefined();
91+
const jsonTree = tree.toJSON() as JsonTree;
92+
expect(jsonTree.children[0].children).toEqual(['Custom refersh content']);
93+
});
94+
95+
it('should start refresh process on press action', () => {
96+
const { getByText } = render(
97+
<CryptrProvider {...config}>
98+
<CryptrRefreshButton autoHide={false}>
99+
<Text>Custom idp content</Text>
100+
</CryptrRefreshButton>
101+
</CryptrProvider>
102+
);
103+
const item = getByText('Custom idp content');
104+
const getRefreshFn = jest.spyOn(Cryptr, 'getRefresh');
105+
fireEvent.press(item);
106+
expect(getRefreshFn).toHaveBeenCalled();
107+
});
108+
});
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import React from 'react';
2+
import { Text } from 'react-native';
3+
import type { CryptrConfig } from '../../utils/interfaces';
4+
import CryptrProvider from '../../models/CryptrProvider';
5+
import renderer from 'react-test-renderer';
6+
import CryptrSsoSignInButton from '../../components/CryptrSsoSignInButton';
7+
import { Locale } from '../../utils/enums';
8+
import { render, fireEvent } from '@testing-library/react-native';
9+
import Cryptr from '../../models/Cryptr';
10+
interface JsonTree {
11+
children: any;
12+
}
13+
jest.mock('../../models/Cryptr', () => ({
14+
startSecuredView: jest.fn(),
15+
getRefresh: jest.fn(),
16+
setRefresh: jest.fn(),
17+
}));
18+
19+
describe('CryptrSsoSignInButton', () => {
20+
const config: CryptrConfig = {
21+
tenant_domain: 'shark_academy',
22+
client_id: '123',
23+
audience: 'cryptr://audience',
24+
default_redirect_uri: 'cryptr://redirect-uri',
25+
};
26+
27+
it('should mount and allow cryptrsso provider', () => {
28+
const tree = renderer.create(
29+
<CryptrProvider {...config}>
30+
<CryptrSsoSignInButton idpId="12" />
31+
</CryptrProvider>
32+
);
33+
expect(tree).not.toBeUndefined();
34+
const jsonTree = tree.toJSON() as JsonTree;
35+
expect(jsonTree.children[0].children).toEqual(['Sign in with SSO']);
36+
expect(tree.root.findByType(CryptrSsoSignInButton).props).toEqual({
37+
idpId: '12',
38+
});
39+
});
40+
41+
it('should mount and allow french cryptrsso provider', () => {
42+
const tree = renderer.create(
43+
<CryptrProvider {...config} default_locale={Locale.FR}>
44+
<CryptrSsoSignInButton idpId="12" />
45+
</CryptrProvider>
46+
);
47+
expect(tree).not.toBeUndefined();
48+
const jsonTree = tree.toJSON() as JsonTree;
49+
expect(jsonTree.children[0].children).toEqual(['Se connecter en SSO']);
50+
expect(tree.root.findByType(CryptrSsoSignInButton).props).toEqual({
51+
idpId: '12',
52+
});
53+
});
54+
55+
it('should mount and forbid idpidless cryptrsso provider', () => {
56+
expect(() =>
57+
renderer.create(
58+
<CryptrProvider {...config} default_locale={Locale.FR}>
59+
<CryptrSsoSignInButton idpId="" />
60+
</CryptrProvider>
61+
)
62+
).toThrow("Please provide non blank value for 'idpId'");
63+
});
64+
65+
it('should mount and allow cryptrsso button with custom test', () => {
66+
const tree = renderer.create(
67+
<CryptrProvider {...config}>
68+
<CryptrSsoSignInButton idpId="12" text="My custom text" />
69+
</CryptrProvider>
70+
);
71+
expect(tree).not.toBeUndefined();
72+
const jsonTree = tree.toJSON() as JsonTree;
73+
expect(jsonTree.children[0].children).toEqual(['My custom text']);
74+
});
75+
76+
it('should mount and allow cryptrsso button with custom children', () => {
77+
const tree = renderer.create(
78+
<CryptrProvider {...config}>
79+
<CryptrSsoSignInButton idpId="12">
80+
<Text>Custom idp content</Text>
81+
</CryptrSsoSignInButton>
82+
</CryptrProvider>
83+
);
84+
expect(tree).not.toBeUndefined();
85+
const jsonTree = tree.toJSON() as JsonTree;
86+
expect(jsonTree.children[0].children).toEqual(['Custom idp content']);
87+
});
88+
89+
it('should start sso process on press action', () => {
90+
const { getByText } = render(
91+
<CryptrProvider {...config}>
92+
<CryptrSsoSignInButton idpId="app_sso_idp_id">
93+
<Text>Custom idp content</Text>
94+
</CryptrSsoSignInButton>
95+
</CryptrProvider>
96+
);
97+
const item = getByText('Custom idp content');
98+
const startSecuredViewFn = jest.spyOn(Cryptr, 'startSecuredView');
99+
fireEvent.press(item);
100+
expect(startSecuredViewFn).toHaveBeenCalledWith(
101+
expect.stringMatching(
102+
'https://auth.cryptr.eu/enterprise/app_sso_idp_id/login'
103+
),
104+
expect.anything(),
105+
expect.anything()
106+
);
107+
});
108+
});
Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
11
import React from 'react';
2+
import { Text } from 'react-native';
23
import type { CryptrConfig } from '../../utils/interfaces';
34
import CryptrProvider from '../../models/CryptrProvider';
45
import renderer from 'react-test-renderer';
5-
import CryptrSsoSignInButton from '../../components/CryptrSsoSignInButton';
6+
import useCryptr from '../../useCryptr';
7+
interface JsonTree {
8+
children: any;
9+
}
10+
11+
const LoadingComponent = () => {
12+
const { isLoading } = useCryptr();
13+
14+
return (
15+
<Text testID="loading">{isLoading ? 'Loading...' : 'Not Loading'}</Text>
16+
);
17+
};
18+
19+
const AuthenticationComponent = () => {
20+
const { isAuthenticated } = useCryptr();
21+
22+
return (
23+
<Text testID="loading">
24+
{isAuthenticated ? 'Authenticated' : 'Unauthenticated'}
25+
</Text>
26+
);
27+
};
628

729
describe('CryptrProvider', () => {
830
const config: CryptrConfig = {
@@ -11,13 +33,37 @@ describe('CryptrProvider', () => {
1133
audience: 'cryptr://audience',
1234
default_redirect_uri: 'cryptr://redirect-uri',
1335
};
14-
it('should mount', () => {
36+
37+
it('should mount as simple', () => {
38+
const tree = renderer.create(
39+
<CryptrProvider {...config}>
40+
<Text>Cryptr child</Text>
41+
</CryptrProvider>
42+
);
43+
expect(tree).not.toBeUndefined();
44+
const jsonTree = tree.toJSON() as JsonTree;
45+
expect(jsonTree.children).toEqual(['Cryptr child']);
46+
});
47+
48+
it('should mount with initial loading state', () => {
49+
const tree = renderer.create(
50+
<CryptrProvider {...config}>
51+
<LoadingComponent />
52+
</CryptrProvider>
53+
);
54+
expect(tree).not.toBeUndefined();
55+
const jsonTree = tree.toJSON() as JsonTree;
56+
expect(jsonTree.children).toEqual(['Not Loading']);
57+
});
58+
59+
it('should mount with initial authneticated state', () => {
1560
const tree = renderer.create(
1661
<CryptrProvider {...config}>
17-
<CryptrSsoSignInButton idpId="12" />
62+
<AuthenticationComponent />
1863
</CryptrProvider>
1964
);
2065
expect(tree).not.toBeUndefined();
21-
// console.debug(tree.toJSON()?.children);
66+
const jsonTree = tree.toJSON() as JsonTree;
67+
expect(jsonTree.children).toEqual(['Unauthenticated']);
2268
});
2369
});

0 commit comments

Comments
 (0)