Skip to content

Commit 19b591a

Browse files
authored
Add jest test suite for TS files (#28)
1 parent bed54a2 commit 19b591a

File tree

15 files changed

+298
-77
lines changed

15 files changed

+298
-77
lines changed

.github/workflows/ci.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ jobs:
3939

4040
- run: ./scripts/compare_snapshot
4141

42+
test:
43+
name: Run jest tests
44+
runs-on: shopify-ubuntu-latest
45+
steps:
46+
- uses: actions/checkout@v3
47+
48+
- name: Setup
49+
uses: ./.github/actions/setup
50+
51+
- run: yarn test
52+
4253
build-android:
4354
name: Build Android Sample App
4455
runs-on: shopify-ubuntu-latest
@@ -74,7 +85,8 @@ jobs:
7485
~/.gradle/caches
7586
sample/android/.gradle/wrapper
7687
sample/android/.gradle/caches
77-
key: ${{ runner.os }}-gradle-${{ hashFiles('sample/android/gradle/wrapper/gradle-wrapper.properties') }}
88+
key:
89+
${{ runner.os }}-gradle-${{ hashFiles('sample/android/gradle/wrapper/gradle-wrapper.properties') }}
7890
restore-keys: |
7991
${{ runner.os }}-gradle-
8092

babel.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
presets: ['module:metro-react-native-babel-preset'],
3+
plugins: ['module:react-native-dotenv'],
4+
};

jest.config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
preset: 'react-native',
3+
modulePathIgnorePatterns: ['modules/react-native-shopify-checkout-kit/lib'],
4+
transform: {
5+
'\\.[jt]sx?$': 'babel-jest',
6+
},
7+
globals: {
8+
'ts-jest': {
9+
tsConfig: {
10+
importHelpers: true,
11+
},
12+
},
13+
},
14+
};

modules/react-native-shopify-checkout-kit/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"license": "MIT",
44
"version": "0.1.0",
55
"main": "lib/commonjs/index.js",
6-
"types": "src/index.tsx",
7-
"source": "src/index.tsx",
6+
"types": "src/index.ts",
7+
"source": "src/index.ts",
88
"module": "lib/module/index.js",
99
"description": "A React Native library for Shopify's Checkout Kit.",
1010
"author": "Shopify",

modules/react-native-shopify-checkout-kit/package.snap renamed to modules/react-native-shopify-checkout-kit/package.snapshot.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
"react-native-shopify-checkout-kit.podspec",
3232
"src/context.tsx",
3333
"src/index.d.ts",
34-
"src/index.tsx"
34+
"src/index.ts"
3535
]

modules/react-native-shopify-checkout-kit/src/index.tsx renamed to modules/react-native-shopify-checkout-kit/src/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ import {
2626
NativeEventEmitter,
2727
EmitterSubscription,
2828
} from 'react-native';
29+
import type {ShopifyCheckoutKit as ShopifyCheckout} from '.';
30+
import {ColorScheme} from './index.d';
31+
import {ShopifyCheckoutKitProvider, useShopifyCheckoutKit} from './context';
2932
import type {
3033
CheckoutEvent,
3134
CheckoutEventCallback,
3235
Configuration,
33-
ShopifyCheckoutKit as ShopifyCheckout,
3436
} from './index.d';
35-
import {ColorScheme} from './index.d';
36-
import {ShopifyCheckoutKitProvider, useShopifyCheckoutKit} from './context';
3737

3838
const RNShopifyCheckoutKit = NativeModules.ShopifyCheckoutKit;
3939

@@ -87,11 +87,11 @@ class ShopifyCheckoutKit implements ShopifyCheckout {
8787

8888
// API
8989
export {
90-
ColorScheme,
9190
ShopifyCheckoutKit,
9291
ShopifyCheckoutKitProvider,
9392
useShopifyCheckoutKit,
93+
ColorScheme,
9494
};
9595

9696
// Types
97-
export type {Configuration, CheckoutEvent};
97+
export {CheckoutEvent, CheckoutEventCallback, Configuration};
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/* eslint-disable no-new */
2+
3+
import {ShopifyCheckoutKit} from '../src';
4+
import {ColorScheme, type Configuration} from '../src';
5+
import {NativeModules} from 'react-native';
6+
7+
const checkoutUrl = 'https://shopify.com/checkout';
8+
const config: Configuration = {
9+
colorScheme: ColorScheme.automatic,
10+
};
11+
12+
jest.mock('react-native', () => {
13+
const NativeEventEmitter = jest.fn(() => ({
14+
addListener: jest.fn(),
15+
removeAllListeners: jest.fn(),
16+
}));
17+
18+
const exampleConfig = {
19+
preloading: true,
20+
};
21+
22+
const ShopifyCheckoutKit = {
23+
version: '0.7.0',
24+
configure: jest.fn(),
25+
preload: jest.fn(),
26+
present: jest.fn(),
27+
getConfig: jest.fn(async () => exampleConfig),
28+
addEventListener: jest.fn(),
29+
removeEventListeners: jest.fn(),
30+
};
31+
32+
return {
33+
NativeEventEmitter,
34+
NativeModules: {
35+
ShopifyCheckoutKit,
36+
},
37+
};
38+
});
39+
40+
describe('ShopifyCheckoutKit', () => {
41+
afterEach(() => {
42+
NativeModules.ShopifyCheckoutKit.configure.mockReset();
43+
});
44+
45+
describe('instantiation', () => {
46+
it('calls `configure` with the specified config on instantiation', () => {
47+
new ShopifyCheckoutKit(config);
48+
expect(NativeModules.ShopifyCheckoutKit.configure).toHaveBeenCalledWith(
49+
config,
50+
);
51+
});
52+
53+
it('does not call `configure` if no config was specified on instantiation', () => {
54+
new ShopifyCheckoutKit();
55+
expect(NativeModules.ShopifyCheckoutKit.configure).not.toHaveBeenCalled();
56+
});
57+
});
58+
59+
describe('configure', () => {
60+
it('calls the `configure` on the Native Module', () => {
61+
const instance = new ShopifyCheckoutKit();
62+
instance.configure(config);
63+
expect(NativeModules.ShopifyCheckoutKit.configure).toHaveBeenCalledTimes(
64+
1,
65+
);
66+
expect(NativeModules.ShopifyCheckoutKit.configure).toHaveBeenCalledWith(
67+
config,
68+
);
69+
});
70+
});
71+
72+
describe('preload', () => {
73+
it('calls `preload` with a checkout URL', () => {
74+
const instance = new ShopifyCheckoutKit();
75+
instance.preload(checkoutUrl);
76+
expect(NativeModules.ShopifyCheckoutKit.preload).toHaveBeenCalledTimes(1);
77+
expect(NativeModules.ShopifyCheckoutKit.preload).toHaveBeenCalledWith(
78+
checkoutUrl,
79+
);
80+
});
81+
});
82+
83+
describe('present', () => {
84+
it('calls `present` with a checkout URL', () => {
85+
const instance = new ShopifyCheckoutKit();
86+
instance.present(checkoutUrl);
87+
expect(NativeModules.ShopifyCheckoutKit.present).toHaveBeenCalledTimes(1);
88+
expect(NativeModules.ShopifyCheckoutKit.present).toHaveBeenCalledWith(
89+
checkoutUrl,
90+
);
91+
});
92+
});
93+
94+
describe('getConfig', () => {
95+
it('returns the config from the Native Module', async () => {
96+
const instance = new ShopifyCheckoutKit();
97+
await expect(instance.getConfig()).resolves.toStrictEqual({
98+
preloading: true,
99+
});
100+
expect(NativeModules.ShopifyCheckoutKit.getConfig).toHaveBeenCalledTimes(
101+
1,
102+
);
103+
});
104+
});
105+
106+
describe('addEventListener', () => {
107+
it('creates a new event listener for a specific event', () => {
108+
const instance = new ShopifyCheckoutKit();
109+
const eventName = 'close';
110+
const callback = jest.fn();
111+
instance.addEventListener(eventName, callback);
112+
// @ts-expect-error
113+
expect(instance.eventEmitter.addListener).toHaveBeenCalledWith(
114+
eventName,
115+
callback,
116+
);
117+
});
118+
});
119+
120+
describe('removeEventListeners', () => {
121+
it('Removes all listeners for a specific event', () => {
122+
const instance = new ShopifyCheckoutKit();
123+
instance.addEventListener('close', () => {});
124+
instance.addEventListener('close', () => {});
125+
instance.removeEventListeners('close');
126+
// @ts-expect-error
127+
expect(instance.eventEmitter.removeAllListeners).toHaveBeenCalledWith(
128+
'close',
129+
);
130+
});
131+
});
132+
});

package.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
"module": "yarn workspace react-native-shopify-checkout-kit",
2121
"pod-install": "NO_FLIPPER=1 pod-install",
2222
"snapshot": "./scripts/create_snapshot",
23-
"turbo": "turbo"
23+
"turbo": "turbo",
24+
"test": "jest"
25+
},
26+
"dependencies": {
27+
"react": "^18.2.0"
2428
},
2529
"devDependencies": {
2630
"@babel/core": "^7.20.0",
@@ -29,10 +33,11 @@
2933
"@react-native/eslint-config": "^0.72.2",
3034
"@react-native/metro-config": "^0.72.11",
3135
"@tsconfig/react-native": "^3.0.0",
36+
"@types/jest": "^29.5.10",
3237
"@types/react": "^18.0.24",
3338
"@types/react-native-dotenv": "^0.2.1",
3439
"@types/react-test-renderer": "^18.0.0",
35-
"babel-jest": "^29.2.1",
40+
"babel-jest": "^29.7.0",
3641
"eslint": "^8.19.0",
3742
"jest": "^29.2.1",
3843
"pod-install": "^0.1.39",
@@ -41,13 +46,10 @@
4146
"react-native-dotenv": "^3.4.9",
4247
"react-native-gradle-plugin": "^0.71.19",
4348
"react-test-renderer": "18.2.0",
49+
"ts-jest": "^29.1.1",
4450
"turbo": "^1.10.16",
4551
"typescript": "4.8.4"
4652
},
47-
"peerDependencies": {
48-
"react": "*",
49-
"react-native": "*"
50-
},
5153
"engines": {
5254
"node": ">=18"
5355
},

sample/jest.config.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

sample/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"@react-navigation/bottom-tabs": "^6.5.11",
2020
"@react-navigation/native": "^6.1.9",
2121
"graphql": "^16.8.1",
22-
"react": "18.2.0",
2322
"react-native": "0.72.6",
2423
"react-native-dotenv": "^3.4.9",
2524
"react-native-gesture-handler": "^2.13.4",
@@ -29,6 +28,9 @@
2928
"react-native-shopify-checkout-kit": "*",
3029
"react-native-vector-icons": "^10.0.2"
3130
},
31+
"peerDependencies": {
32+
"react": "*"
33+
},
3234
"devDependencies": {
3335
"@babel/core": "^7.20.0",
3436
"@babel/preset-env": "^7.20.0",

0 commit comments

Comments
 (0)