UNPKG

5.69 kBPlain TextView Raw
1import { Platform } from 'react-native';
2import { PlatformArray } from '../src/internal/privateTypes';
3import {
4 clearMemo,
5 getSupportedPlatformInfoAsync,
6 getSupportedPlatformInfoSync,
7 getSupportedPlatformInfoFunctions,
8} from '../src/internal/supported-platform-info';
9
10describe('supported platform info', () => {
11 describe('clearMemo', () => {
12 it('should exist as a function', () => {
13 expect(typeof clearMemo).toEqual('function');
14 });
15 });
16
17 describe('getSupportedPlatformInfoSync', () => {
18 const getterResponse = Math.random();
19 const getter = jest.fn().mockReturnValue(getterResponse);
20 const supportedPlatforms: PlatformArray = ['ios', 'android'];
21
22 beforeEach(() => {
23 getter.mockClear();
24 });
25
26 it('should exist as a function', () => {
27 expect(typeof getSupportedPlatformInfoSync).toEqual('function');
28 });
29
30 it.each(supportedPlatforms)('should call getter param for platform, %s', (platform) => {
31 Platform.OS = platform;
32 const output = getSupportedPlatformInfoSync({
33 getter,
34 supportedPlatforms,
35 defaultValue: -1,
36 });
37 expect(getter).toHaveBeenCalled();
38 expect(output).toBe(getterResponse);
39 });
40
41 it('should return the default value and not call getter if OS is not in supported platforms', () => {
42 Platform.OS = 'windows';
43 const defaultValue = Math.random() * 10;
44 const output = getSupportedPlatformInfoSync({
45 getter,
46 supportedPlatforms,
47 defaultValue,
48 });
49
50 expect(output).toBe(defaultValue);
51 expect(getter).not.toHaveBeenCalled();
52 });
53
54 it('should use memo if key is passed', () => {
55 clearMemo();
56 Platform.OS = 'ios';
57 const defaultValue = Math.random() * 10;
58 const memoKey = `getSupportedPlatformInfoSync`;
59 const outputA = getSupportedPlatformInfoSync({
60 getter,
61 supportedPlatforms,
62 defaultValue,
63 memoKey,
64 });
65
66 expect(outputA).toBe(getterResponse);
67 expect(getter).toHaveBeenCalled();
68 getter.mockClear();
69
70 const outputB = getSupportedPlatformInfoSync({
71 getter,
72 supportedPlatforms,
73 defaultValue,
74 memoKey,
75 });
76
77 expect(outputB).toBe(getterResponse);
78 expect(getter).not.toHaveBeenCalled();
79 });
80 });
81
82 describe('getSupportedPlatformInfoAsync', () => {
83 const getterResponse = Math.random();
84 const getter = jest.fn(() => Promise.resolve(getterResponse));
85 const supportedPlatforms: PlatformArray = ['ios', 'android'];
86
87 beforeEach(() => {
88 getter.mockClear();
89 });
90
91 it('should exist as a function', () => {
92 expect(typeof getSupportedPlatformInfoAsync).toBe('function');
93 });
94
95 it.each(supportedPlatforms)('should call getter param for platform, %s', async (platform) => {
96 Platform.OS = platform;
97 const output = await getSupportedPlatformInfoAsync({
98 getter,
99 supportedPlatforms,
100 defaultValue: -1,
101 });
102 expect(getter).toHaveBeenCalled();
103 expect(output).toBe(getterResponse);
104 });
105
106 it('should return the default value and not call getter if OS is not in supported platforms', async () => {
107 Platform.OS = 'windows';
108 const defaultValue = Math.random() * 10;
109 const output = await getSupportedPlatformInfoAsync({
110 getter,
111 supportedPlatforms,
112 defaultValue,
113 });
114
115 expect(output).toBe(defaultValue);
116 expect(getter).not.toHaveBeenCalled();
117 });
118
119 it('should use memo if key is passed', async () => {
120 clearMemo();
121 Platform.OS = 'ios';
122 const defaultValue = Math.random() * 10;
123 const memoKey = `getSupportedPlatformInfoAsync`;
124 const outputA = await getSupportedPlatformInfoAsync({
125 getter,
126 supportedPlatforms,
127 defaultValue,
128 memoKey,
129 });
130
131 expect(outputA).toBe(getterResponse);
132 expect(getter).toHaveBeenCalled();
133 getter.mockClear();
134
135 const outputB = await getSupportedPlatformInfoAsync({
136 getter,
137 supportedPlatforms,
138 defaultValue,
139 memoKey,
140 });
141
142 expect(outputB).toBe(getterResponse);
143 expect(getter).not.toHaveBeenCalled();
144 });
145 });
146
147 describe('getSupportedPlatformInfoFunctions', () => {
148 const defaultValue = -1;
149 const supportedPlatforms: PlatformArray = ['ios', 'android'];
150 const getterResponse = Math.random();
151 const getter = jest.fn(() => Promise.resolve(getterResponse));
152 const syncGetter = jest.fn().mockReturnValue(getterResponse);
153
154 const generatedFns = getSupportedPlatformInfoFunctions({
155 defaultValue,
156 supportedPlatforms,
157 getter,
158 syncGetter,
159 });
160
161 beforeEach(() => {
162 getter.mockClear();
163 syncGetter.mockClear();
164 clearMemo();
165 });
166
167 it('should exist as a function', () => {
168 expect(typeof getSupportedPlatformInfoFunctions).toEqual('function');
169 });
170
171 it('should return back an array of functions', () => {
172 expect(Array.isArray(generatedFns)).toEqual(true);
173 expect(generatedFns.length).toEqual(2);
174 generatedFns.forEach((fn) => {
175 expect(typeof fn).toEqual('function');
176 });
177 });
178
179 it('should have first getter be an async function that returns expected object', async () => {
180 const resp = await generatedFns[0]();
181 expect(getter).toBeCalled();
182 expect(resp).toEqual(getterResponse);
183 });
184
185 it('should have second getter be a sync function that returns expected object', () => {
186 const resp = generatedFns[1]();
187 expect(syncGetter).toBeCalled();
188 expect(resp).toEqual(getterResponse);
189 });
190 });
191});