|
1 | | -import { CallOptions, PenpalError } from '../../src/index.js'; |
2 | 1 | import type { Methods } from '../../src/index.js'; |
3 | 2 | import FixtureMethods from '../childFixtures/types/FixtureMethods.js'; |
| 3 | +import { runCommunicationContract } from '../contracts/communicationContract.js'; |
4 | 4 | import { createBackwardCompatibilityIframeAndConnection } from './utils.js'; |
5 | 5 |
|
6 | | -const createConnection = (methods?: Methods) => { |
| 6 | +const createConnection = (options?: { methods?: Methods }) => { |
7 | 7 | return createBackwardCompatibilityIframeAndConnection<FixtureMethods>({ |
8 | | - methods, |
| 8 | + methods: options?.methods, |
9 | 9 | }).connection; |
10 | 10 | }; |
11 | 11 |
|
12 | | -describe(`BACKWARD COMPATIBILITY: communication between parent and child iframe`, () => { |
13 | | - it('calls a function on the child', async () => { |
14 | | - const connection = createConnection(); |
15 | | - const child = await connection.promise; |
16 | | - const value = await child.multiply(2, 5); |
17 | | - expect(value).toEqual(10); |
18 | | - connection.destroy(); |
19 | | - }); |
20 | | - |
21 | | - it('calls nested functions on the child', async () => { |
22 | | - const connection = createConnection(); |
23 | | - const child = await connection.promise; |
24 | | - const oneLevel = await child.nested.oneLevel('pen'); |
25 | | - expect(oneLevel).toEqual('pen'); |
26 | | - const twoLevels = await child.nested.by.twoLevels('pal'); |
27 | | - expect(twoLevels).toEqual('pal'); |
28 | | - connection.destroy(); |
29 | | - }); |
30 | | - |
31 | | - it('calls an asynchronous function on the child', async () => { |
32 | | - const connection = createConnection(); |
33 | | - const child = await connection.promise; |
34 | | - const value = await child.multiplyAsync(2, 5); |
35 | | - expect(value).toEqual(10); |
36 | | - connection.destroy(); |
37 | | - }); |
38 | | - |
39 | | - it('calls a function on the parent', async () => { |
40 | | - const connection = createConnection({ |
41 | | - add: (num1: number, num2: number) => { |
42 | | - return num1 + num2; |
43 | | - }, |
44 | | - }); |
45 | | - const child = await connection.promise; |
46 | | - await child.addUsingParent(); |
47 | | - const value = await child.getParentReturnValue(); |
48 | | - expect(value).toEqual(9); |
49 | | - connection.destroy(); |
50 | | - }); |
51 | | - |
52 | | - it('handles promises rejected with strings', async () => { |
53 | | - const connection = createConnection(); |
54 | | - const child = await connection.promise; |
55 | | - await expect(child.getPromiseRejectedWithString()).rejects.toBe( |
56 | | - 'test error string' |
57 | | - ); |
58 | | - connection.destroy(); |
59 | | - }); |
60 | | - |
61 | | - it('handles promises rejected with objects', async () => { |
62 | | - const connection = createConnection(); |
63 | | - const child = await connection.promise; |
64 | | - let error; |
65 | | - try { |
66 | | - await child.getPromiseRejectedWithObject(); |
67 | | - } catch (e) { |
68 | | - error = e; |
69 | | - } |
70 | | - expect(error).toEqual({ a: 'b' }); |
71 | | - connection.destroy(); |
72 | | - }); |
73 | | - |
74 | | - it('handles promises rejected with undefined', async () => { |
75 | | - const connection = createConnection(); |
76 | | - const child = await connection.promise; |
77 | | - let catchCalled = false; |
78 | | - let error; |
79 | | - try { |
80 | | - await child.getPromiseRejectedWithUndefined(); |
81 | | - } catch (e) { |
82 | | - catchCalled = true; |
83 | | - error = e; |
84 | | - } |
85 | | - expect(catchCalled).toBe(true); |
86 | | - expect(error).toBeUndefined(); |
87 | | - connection.destroy(); |
88 | | - }); |
89 | | - |
90 | | - it('handles promises rejected with error objects', async () => { |
91 | | - const connection = createConnection(); |
92 | | - const child = await connection.promise; |
93 | | - let error; |
94 | | - try { |
95 | | - await child.getPromiseRejectedWithError(); |
96 | | - } catch (e) { |
97 | | - error = e; |
98 | | - } |
99 | | - expect(error).toEqual(expect.any(Error)); |
100 | | - expect((error as Error).name).toBe('TypeError'); |
101 | | - expect((error as Error).message).toBe('test error object'); |
102 | | - expect((error as Error).stack).toEqual(expect.any(String)); |
103 | | - connection.destroy(); |
104 | | - }); |
105 | | - |
106 | | - it('handles thrown errors', async () => { |
107 | | - const connection = createConnection(); |
108 | | - const child = await connection.promise; |
109 | | - let error; |
110 | | - try { |
111 | | - await child.throwError(); |
112 | | - } catch (e) { |
113 | | - error = e; |
114 | | - } |
115 | | - expect(error).toEqual(expect.any(Error)); |
116 | | - expect((error as Error).message).toBe('Oh nos!'); |
117 | | - connection.destroy(); |
118 | | - }); |
119 | | - |
120 | | - it('handles unclonable values', async () => { |
121 | | - const connection = createConnection(); |
122 | | - const child = await connection.promise; |
123 | | - let error; |
124 | | - try { |
125 | | - await child.getUnclonableValue(); |
126 | | - } catch (e) { |
127 | | - error = e; |
128 | | - } |
129 | | - expect(error).toEqual(expect.any(Error)); |
130 | | - expect((error as Error).name).toBe('DataCloneError'); |
131 | | - connection.destroy(); |
132 | | - }); |
133 | | - |
134 | | - it('rejects method call promise if method call timeout reached', async () => { |
135 | | - const connection = createConnection(); |
136 | | - const child = await connection.promise; |
137 | | - const promise = child.neverResolve( |
138 | | - new CallOptions({ |
139 | | - timeout: 0, |
140 | | - }) |
141 | | - ); |
142 | | - |
143 | | - let error; |
144 | | - try { |
145 | | - await promise; |
146 | | - } catch (e) { |
147 | | - error = e; |
148 | | - } |
149 | | - |
150 | | - expect(error).toEqual(expect.any(Error)); |
151 | | - expect((error as Error).message).toBe( |
152 | | - 'Method call neverResolve() timed out after 0ms' |
153 | | - ); |
154 | | - expect((error as PenpalError).code).toBe('METHOD_CALL_TIMEOUT'); |
155 | | - connection.destroy(); |
156 | | - }); |
157 | | - |
158 | | - it('rejects method call promise if connection is destroyed before reply is received', async () => { |
159 | | - const connection = createConnection(); |
160 | | - const child = await connection.promise; |
161 | | - |
162 | | - let error: Error; |
163 | | - |
164 | | - child.neverResolve().catch((e) => { |
165 | | - error = e; |
166 | | - }); |
167 | | - connection.destroy(); |
168 | | - |
169 | | - // Wait for microtask queue to drain |
170 | | - await Promise.resolve(); |
171 | | - |
172 | | - expect(error!).toEqual(expect.any(Error)); |
173 | | - expect(error!.message).toBe( |
174 | | - 'Method call neverResolve() failed due to destroyed connection' |
175 | | - ); |
176 | | - expect((error! as PenpalError).code).toBe('CONNECTION_DESTROYED'); |
177 | | - connection.destroy(); |
178 | | - }); |
| 12 | +runCommunicationContract({ |
| 13 | + suiteName: |
| 14 | + 'BACKWARD COMPATIBILITY: communication between parent and child iframe', |
| 15 | + createConnection, |
| 16 | + includeAdvancedCases: false, |
179 | 17 | }); |
0 commit comments