|
1 |
| -const hmmBuilder = require('./builder') |
| 1 | +const HmmBuild = require("./builder"); |
| 2 | + |
| 3 | +const delay = ms => new Promise(rs => setTimeout(rs, ms)) |
| 4 | +const fetchOk = (url, data) => new Promise((resolve, reject) => resolve(data)) |
| 5 | +const fetchNOk = (url, data) => new Promise((resolve, reject) => reject('something bad')) |
2 | 6 |
|
3 | 7 | describe('hmm-builder', function () {
|
4 |
| - it('should call send method', async () => { |
5 |
| - const mockPost = jest.fn(payload => "RESPONSE:" + JSON.stringify(payload)) |
6 |
| - const Model = hmmBuilder(mockPost) |
7 |
| - const UserModel = Model('user') |
8 |
| - const rs = await UserModel.find({_id: 1}, { username: 1, password: 1 }).sort({createdDate: -1}).limit(10) |
9 |
| - expect(mockPost.mock.calls.length).toBe(1) |
| 8 | + describe('send', function () { |
| 9 | + it('should fail on sync send method', async () => { |
| 10 | + const send = payload => "RESPONSE:" + JSON.stringify(payload) |
| 11 | + const mockSend = jest.fn(send) |
| 12 | + const Model = HmmBuild(mockSend) |
| 13 | + let throwException; |
| 14 | + try { |
| 15 | + await Model('user').find({_id: 1}, { username: 1, password: 1 }).sort({createdDate: -1}).limit(10) |
| 16 | + throwException = false; |
| 17 | + } catch (e) { |
| 18 | + throwException = true; |
| 19 | + } |
| 20 | + expect(mockSend.mock.calls.length).toBe(1) |
| 21 | + expect(throwException).toBe(true) |
| 22 | + }) |
| 23 | + it('should work on async send method', async () => { |
| 24 | + const send = async payload => "RESPONSE:" + JSON.stringify(payload) |
| 25 | + const mockSend = jest.fn(send) |
| 26 | + const Model = HmmBuild(mockSend) |
| 27 | + let throwException; |
| 28 | + try { |
| 29 | + await Model('user').find({_id: 1}, { username: 1, password: 1 }).sort({createdDate: -1}).limit(10) |
| 30 | + throwException = false; |
| 31 | + } catch (e) { |
| 32 | + throwException = true; |
| 33 | + } |
| 34 | + expect(mockSend.mock.calls.length).toBe(1) |
| 35 | + expect(throwException).toBe(false) |
| 36 | + }) |
| 37 | + it('should work on Promise send method', async () => { |
| 38 | + const send = payload => new Promise(resolve => resolve("RESPONSE:" + JSON.stringify(payload))) |
| 39 | + const mockSend = jest.fn(send) |
| 40 | + const Model = HmmBuild(mockSend) |
| 41 | + let throwException; |
| 42 | + try { |
| 43 | + await Model('user').find({_id: 1}, { username: 1, password: 1 }).sort({createdDate: -1}).limit(10) |
| 44 | + throwException = false; |
| 45 | + } catch (e) { |
| 46 | + throwException = true; |
| 47 | + } |
| 48 | + expect(mockSend.mock.calls.length).toBe(1) |
| 49 | + expect(throwException).toBe(false) |
| 50 | + }) |
| 51 | + }) |
| 52 | + describe('usage', function () { |
| 53 | + describe('invoke', function () { |
| 54 | + describe('Promise', () => { |
| 55 | + it('should work with Promise#then', async () => { |
| 56 | + const send = data => fetchOk('/execute', data) |
| 57 | + const Model = HmmBuild(send) |
| 58 | + const datas = [] |
| 59 | + Model('file').updateOne({name: 'x'}, {age: 1}).then(data => datas.push(data)) |
| 60 | + await delay(10) |
| 61 | + expect(datas.length).toBe(1) |
| 62 | + }) |
| 63 | + it('should catch exception with Promise#catch', async () => { |
| 64 | + const send = data => fetchNOk('/execute', data) |
| 65 | + const Model = HmmBuild(send) |
| 66 | + const datas = [] |
| 67 | + const errors = [] |
| 68 | + Model('file').updateOne({name: 'x'}, {age: 1}).catch(e => errors.push(e)) |
| 69 | + await delay(10) |
| 70 | + expect(datas.length).toBe(0) |
| 71 | + expect(errors.length).toBe(1) |
| 72 | + }) |
| 73 | + it('should work with Promise#then#catch (1)', async () => { |
| 74 | + const send = data => fetchOk('/execute', data) |
| 75 | + const Model = HmmBuild(send) |
| 76 | + const datas = [] |
| 77 | + const errors = [] |
| 78 | + Model('file').updateOne({name: 'x'}, {age: 1}).then(data => datas.push(data)).catch(e => errors.push(e)) |
| 79 | + await delay(10) |
| 80 | + expect(datas.length).toBe(1) |
| 81 | + expect(errors.length).toBe(0) |
| 82 | + }) |
| 83 | + it('should work with Promise#then#catch (1)', async () => { |
| 84 | + const send = data => fetchNOk('/execute', data) |
| 85 | + const Model = HmmBuild(send) |
| 86 | + const datas = [] |
| 87 | + const errors = [] |
| 88 | + Model('file').updateOne({name: 'x'}, {age: 1}).then(data => datas.push(data)).catch(e => errors.push(e)) |
| 89 | + await delay(10) |
| 90 | + expect(datas.length).toBe(0) |
| 91 | + expect(errors.length).toBe(1) |
| 92 | + }) |
| 93 | + }); |
| 94 | + |
| 95 | + describe('async/await', () => { |
| 96 | + it('should work', async () => { |
| 97 | + const send = data => fetchOk('/execute', data) |
| 98 | + const Model = HmmBuild(send) |
| 99 | + const datas = [] |
| 100 | + const errors = [] |
| 101 | + try { |
| 102 | + const data = await Model('file').updateOne({name: 'x'}, {age: 1}) |
| 103 | + datas.push(data) |
| 104 | + } catch (e) { |
| 105 | + errors.push(e) |
| 106 | + } |
| 107 | + expect(datas.length).toBe(1) |
| 108 | + expect(errors.length).toBe(0) |
| 109 | + }) |
| 110 | + it('should catch exception', async () => { |
| 111 | + const send = data => fetchNOk('/execute', data) |
| 112 | + const Model = HmmBuild(send) |
| 113 | + const datas = [] |
| 114 | + const errors = [] |
| 115 | + try { |
| 116 | + const data = await Model('file').updateOne({name: 'x'}, {age: 1}) |
| 117 | + datas.push(data) |
| 118 | + } catch (e) { |
| 119 | + errors.push(e) |
| 120 | + } |
| 121 | + expect(datas.length).toBe(0) |
| 122 | + expect(errors.length).toBe(1) |
| 123 | + }) |
| 124 | + }) |
| 125 | + }) |
| 126 | + |
| 127 | + describe('payload', function() { |
| 128 | + it('should correct when using isolate Model instance', async () => { |
| 129 | + const send = data => fetchOk('/execute', data) |
| 130 | + const Model = HmmBuild(send) |
| 131 | + const datas = {} |
| 132 | + Model('file').updateOne({name: 'x'}, {age: 1}).then(data => datas[0] = data) |
| 133 | + Model('file').find({age: {$lte: 20}}).sort({date: -1}).skip(20).limit(10).then(data => datas[1] = data) |
| 134 | + Model('file').find({name: 'Josh'}).sort({age: 1}).limit(100).then(data => datas[2] = data) |
| 135 | + await delay(50) |
| 136 | + // console.log(datas) |
| 137 | + expect(datas[0].fns.length).toBe(1) |
| 138 | + expect(datas[1].fns.length).toBe(4) |
| 139 | + expect(datas[2].fns.length).toBe(3) |
| 140 | + }) |
| 141 | + it('should messed-up when using shared Model instance', async () => { |
| 142 | + const send = data => fetchOk('/execute', data) |
| 143 | + const Model = HmmBuild(send) |
| 144 | + const FileModel = Model('file') // <- shared model instance |
| 145 | + const datas = {} |
| 146 | + FileModel.updateOne({name: 'x'}, {age: 1}).then(data => datas[0] = data) |
| 147 | + FileModel.find({age: {$lte: 20}}).sort({date: -1}).skip(20).limit(10).then(data => datas[1] = data) |
| 148 | + FileModel.find({name: 'Josh'}).sort({age: 1}).limit(100).then(data => datas[2] = data) |
| 149 | + await delay(50) |
| 150 | + // console.log(datas) |
| 151 | + expect(datas[0].fns.length).not.toBe(1) |
| 152 | + expect(datas[1].fns.length).not.toBe(4) |
| 153 | + expect(datas[2].fns.length).not.toBe(3) |
| 154 | + }) |
| 155 | + }) |
10 | 156 | })
|
11 | 157 | })
|
0 commit comments