|
| 1 | +import EventEmitter from 'events'; |
1 | 2 | import { createSimpleContext, SimpleContext } from '..';
|
2 | 3 |
|
3 | 4 | describe('SimpleContext', () => {
|
@@ -129,4 +130,68 @@ describe('SimpleContext', () => {
|
129 | 130 | ]);
|
130 | 131 | expect(res).toStrictEqual(['foo=tata', 'foo=toto', 'foo=titi', 'foo=tutu']);
|
131 | 132 | });
|
| 133 | + |
| 134 | + it('should work if the values are wrapped in a callback run', async () => { |
| 135 | + function Deferred() { |
| 136 | + let resolve; |
| 137 | + let reject; |
| 138 | + const promise = new Promise((thisResolve, thisReject) => { |
| 139 | + resolve = thisResolve; |
| 140 | + reject = thisReject; |
| 141 | + }); |
| 142 | + return Object.assign(promise, { resolve, reject }); |
| 143 | + } |
| 144 | + const emitter = new EventEmitter(); |
| 145 | + |
| 146 | + const context = createSimpleContext(); |
| 147 | + |
| 148 | + const deferredTiti: any = Deferred(); |
| 149 | + const deferredToto: any = Deferred(); |
| 150 | + emitter.on('titi', () => { |
| 151 | + context.fork(() => { |
| 152 | + context.set('foo', 'titi2'); |
| 153 | + }); |
| 154 | + }); |
| 155 | + emitter.on('toto', () => { |
| 156 | + context.fork(() => { |
| 157 | + context.set('foo', 'toto2'); |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + setTimeout(() => { |
| 162 | + context.fork(() => { |
| 163 | + context.set('foo', 'titi'); |
| 164 | + emitter.emit('titi'); |
| 165 | + setTimeout(() => { |
| 166 | + deferredTiti.resolve(context.get('foo')); |
| 167 | + }, 50); |
| 168 | + }); |
| 169 | + }, 100); |
| 170 | + setTimeout(() => { |
| 171 | + context.fork(() => { |
| 172 | + context.set('foo', 'toto'); |
| 173 | + emitter.emit('toto'); |
| 174 | + setTimeout(() => { |
| 175 | + deferredToto.resolve(context.get('foo')); |
| 176 | + }, 50); |
| 177 | + }); |
| 178 | + }, 200); |
| 179 | + |
| 180 | + expect([await deferredTiti, await deferredToto]).toStrictEqual([ |
| 181 | + 'titi', |
| 182 | + 'toto', |
| 183 | + ]); |
| 184 | + }); |
| 185 | + |
| 186 | + it('should return good value with the callback', async () => { |
| 187 | + const context = createSimpleContext(); |
| 188 | + |
| 189 | + const string = context |
| 190 | + .fork(() => { |
| 191 | + return 'hello'; |
| 192 | + }) |
| 193 | + .toUpperCase(); |
| 194 | + |
| 195 | + expect(string).toBe('HELLO'); |
| 196 | + }); |
132 | 197 | });
|
0 commit comments