-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests
- Loading branch information
Showing
5 changed files
with
311 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/connect-miniprogram/src/connect/async-generator.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { describe, expect, jest, test } from '@jest/globals'; | ||
|
||
import { fireEventQueue } from '../test-utils'; | ||
import { createAsyncGeneratorFromEventPattern } from './async-generator'; | ||
|
||
describe('createAsyncGeneratorFromEventPattern', () => { | ||
const dispose = jest.fn(); | ||
test('creates am async generator', async () => { | ||
const gen = createAsyncGeneratorFromEventPattern<number>( | ||
({ handleValue, handleEnd }) => { | ||
fireEventQueue([ | ||
() => { | ||
handleValue(0); | ||
}, | ||
() => { | ||
handleValue(1); | ||
}, | ||
() => { | ||
handleValue(2); | ||
}, | ||
() => { | ||
handleEnd(); | ||
}, | ||
]); | ||
return dispose; | ||
}, | ||
); | ||
const a = gen(); | ||
expect(dispose).toBeCalledTimes(0); | ||
expect(await a.next()).toEqual({ value: 0, done: false }); | ||
expect(dispose).toBeCalledTimes(0); | ||
expect(await a.next()).toEqual({ value: 1, done: false }); | ||
expect(dispose).toBeCalledTimes(0); | ||
expect(await a.next()).toEqual({ value: 2, done: false }); | ||
expect(dispose).toBeCalledTimes(0); | ||
expect(await a.next()).toEqual({ value: undefined, done: true }); | ||
expect(dispose).toBeCalledTimes(1); | ||
}); | ||
}); |
175 changes: 175 additions & 0 deletions
175
packages/connect-miniprogram/src/connect/wx-request.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
import { describe, expect, jest, test } from '@jest/globals'; | ||
|
||
import { mockWxRequest } from '../test-utils'; | ||
import { | ||
createWxRequestAsAsyncGenerator, | ||
createWxRequestAsPromise, | ||
} from './wx-request'; | ||
|
||
jest.mock('./envelope', () => ({ | ||
createEnvelopeAsyncGenerator: (s) => s, | ||
})); | ||
|
||
describe('createWxRequestAsPromise', () => { | ||
test('should return a promise, using binary format', () => { | ||
const wxRequest = mockWxRequest({}); | ||
const request = createWxRequestAsPromise( | ||
{ | ||
request: wxRequest, | ||
requestOptions: { | ||
forceCellularNetwork: true, | ||
}, | ||
}, | ||
true, | ||
); | ||
const header = new Headers(); | ||
header.append('foo', 'bar'); | ||
request({ | ||
url: 'https://example.com', | ||
data: 'data', | ||
method: 'POST', | ||
header, | ||
}); | ||
expect(wxRequest).toBeCalledWith({ | ||
url: 'https://example.com', | ||
data: 'data', | ||
method: 'POST', | ||
header: { | ||
foo: 'bar', | ||
}, | ||
responseType: 'arraybuffer', | ||
forceCellularNetwork: true, | ||
fail: expect.any(Function), | ||
success: expect.any(Function), | ||
}); | ||
}); | ||
}); | ||
|
||
describe('createWxRequestAsAsyncGenerator', () => { | ||
test('should return an async generator, not devtool', async () => { | ||
const wxRequest = mockWxRequest({ | ||
responseHeader: { | ||
'response-header-key': 'response-header-value', | ||
}, | ||
}); | ||
const request = createWxRequestAsAsyncGenerator({ | ||
request: wxRequest, | ||
isDevTool: false, | ||
requestOptions: { | ||
forceCellularNetwork: true, | ||
}, | ||
}); | ||
const reqHeaders = new Headers(); | ||
reqHeaders.append('foo', 'bar'); | ||
const { | ||
header: resHeader, | ||
statusCode, | ||
messageStream, | ||
} = await request({ | ||
url: 'https://example.com', | ||
data: 'data', | ||
method: 'POST', | ||
header: reqHeaders, | ||
}); | ||
expect(wxRequest).toBeCalledWith({ | ||
url: 'https://example.com', | ||
data: 'data', | ||
method: 'POST', | ||
header: { | ||
foo: 'bar', | ||
}, | ||
enableChunked: true, | ||
responseType: 'arraybuffer', | ||
forceCellularNetwork: true, | ||
fail: expect.any(Function), | ||
success: expect.any(Function), | ||
}); | ||
|
||
expect(resHeader.get('response-header-key')).toBe('response-header-value'); | ||
expect(statusCode).toBe(200); | ||
expect(await messageStream.next()).toEqual({ | ||
done: false, | ||
value: new Uint8Array([1, 2, 3]), | ||
}); | ||
expect(await messageStream.next()).toEqual({ | ||
done: false, | ||
value: new Uint8Array([4, 5, 6]), | ||
}); | ||
expect(await messageStream.next()).toEqual({ | ||
done: true, | ||
value: undefined, | ||
}); | ||
}); | ||
|
||
test('should return an async generator, is devtool', async () => { | ||
const wxRequest = mockWxRequest({ | ||
responseHeader: { | ||
'response-header-key': 'response-header-value', | ||
}, | ||
}); | ||
const request = createWxRequestAsAsyncGenerator({ | ||
request: wxRequest, | ||
isDevTool: true, | ||
requestOptions: { | ||
forceCellularNetwork: true, | ||
}, | ||
}); | ||
const reqHeaders = new Headers(); | ||
reqHeaders.append('foo', 'bar'); | ||
const { | ||
header: resHeader, | ||
statusCode, | ||
messageStream, | ||
} = await request({ | ||
url: 'https://example.com', | ||
data: 'data', | ||
method: 'POST', | ||
header: reqHeaders, | ||
}); | ||
expect(wxRequest).toBeCalledWith({ | ||
url: 'https://example.com', | ||
data: 'data', | ||
method: 'POST', | ||
header: { | ||
foo: 'bar', | ||
}, | ||
responseType: 'arraybuffer', | ||
forceCellularNetwork: true, | ||
fail: expect.any(Function), | ||
success: expect.any(Function), | ||
}); | ||
expect(resHeader.get('response-header-key')).toBe('response-header-value'); | ||
expect(statusCode).toBe(200); | ||
expect(await messageStream.next()).toEqual({ | ||
done: false, | ||
value: new Uint8Array([1, 2, 3, 4, 5, 6]), | ||
}); | ||
expect(await messageStream.next()).toEqual({ | ||
done: true, | ||
value: undefined, | ||
}); | ||
}); | ||
|
||
test('should throw if first chunk is not header', async () => { | ||
const wxRequest = mockWxRequest({ | ||
skipHeadersReceivedHandler: true, | ||
}); | ||
const request = createWxRequestAsAsyncGenerator({ | ||
request: wxRequest, | ||
isDevTool: false, | ||
requestOptions: { | ||
forceCellularNetwork: true, | ||
}, | ||
}); | ||
const reqHeaders = new Headers(); | ||
reqHeaders.append('foo', 'bar'); | ||
expect(async () => { | ||
await request({ | ||
url: 'https://example.com', | ||
data: 'data', | ||
method: 'POST', | ||
header: reqHeaders, | ||
}); | ||
}).rejects.toThrow('missing header'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { jest } from '@jest/globals'; | ||
|
||
function wait() { | ||
return new Promise((resolve) => setTimeout(resolve, 0)); | ||
} | ||
|
||
export async function fireEventQueue(fns: (() => void)[]) { | ||
for (const fn of fns) { | ||
await wait(); | ||
fn(); | ||
} | ||
} | ||
|
||
function buffer(arr: number[]) { | ||
return new Uint8Array(arr).buffer; | ||
} | ||
|
||
export const mockWxRequest = ({ | ||
responseHeader = {}, | ||
skipHeadersReceivedHandler = false, | ||
}: { | ||
responseHeader?: Record<string, string>; | ||
skipHeadersReceivedHandler?: boolean; | ||
}) => { | ||
const headerData: Partial<WechatMiniprogram.RequestSuccessCallbackResult> = { | ||
header: responseHeader, | ||
statusCode: 200, | ||
cookies: [], | ||
}; | ||
return jest.fn((options: WechatMiniprogram.RequestOption) => { | ||
let chunkReceivedHandler: undefined | ((res: any) => void); | ||
let headersReceivedHandler: undefined | ((res: any) => void); | ||
|
||
if (options.enableChunked) { | ||
const eventQueue = [ | ||
() => { | ||
if (!skipHeadersReceivedHandler) { | ||
headersReceivedHandler?.(headerData); | ||
} | ||
}, | ||
() => { | ||
chunkReceivedHandler?.({ | ||
data: buffer([1, 2, 3]), | ||
}); | ||
}, | ||
() => { | ||
chunkReceivedHandler?.({ | ||
data: buffer([4, 5, 6]), | ||
}); | ||
}, | ||
() => { | ||
options.success?.( | ||
{} as WechatMiniprogram.RequestSuccessCallbackResult, | ||
); | ||
}, | ||
]; | ||
fireEventQueue(eventQueue); | ||
} else { | ||
fireEventQueue([ | ||
() => { | ||
options.success?.({ | ||
...headerData, | ||
data: buffer([1, 2, 3, 4, 5, 6]), | ||
} as WechatMiniprogram.RequestSuccessCallbackResult); | ||
}, | ||
]); | ||
} | ||
return { | ||
abort: jest.fn(), | ||
onChunkReceived: jest.fn((fn: (res: any) => void) => { | ||
chunkReceivedHandler = fn; | ||
}), | ||
offChunkReceived: jest.fn(() => { | ||
chunkReceivedHandler = undefined; | ||
}), | ||
onHeadersReceived: jest.fn((fn: (res: any) => void) => { | ||
headersReceivedHandler = fn; | ||
}), | ||
offHeadersReceived: jest.fn(() => { | ||
headersReceivedHandler = undefined; | ||
}), | ||
} as WechatMiniprogram.RequestTask; | ||
}) as typeof wx.request; | ||
}; |