|
| 1 | +import _ from 'lodash' |
| 2 | + |
| 3 | +import IPOS from '../main' |
| 4 | + |
| 5 | +import createConnectedInstances from './createConnectedInstances' |
| 6 | +import {exampleChanges, examples} from './testData' |
| 7 | + |
| 8 | +type ValueOf<T> = T[keyof T] |
| 9 | + |
| 10 | +function createFieldsTest( |
| 11 | + initWith: ( |
| 12 | + methods: ValueOf<typeof exampleChanges>, |
| 13 | + setValue: (ipos_for_setting: IPOS) => void, |
| 14 | + probeValue: (ipos_for_setting: IPOS, ipos_for_probing: IPOS) => void |
| 15 | + ) => Promise<void>, |
| 16 | + createLabel: (key: string) => string |
| 17 | +) { |
| 18 | + for (const exampleKey in exampleChanges) { |
| 19 | + if (!examples.hasOwnProperty(exampleKey)) continue |
| 20 | + it(createLabel(exampleKey), async () => { |
| 21 | + const value: ValueOf<typeof examples> = examples[exampleKey as keyof typeof examples] |
| 22 | + const methods: ValueOf<typeof exampleChanges> = exampleChanges[exampleKey as keyof typeof exampleChanges] |
| 23 | + await initWith( |
| 24 | + methods, |
| 25 | + (ipos_for_setting) => { |
| 26 | + ipos_for_setting.create('myField', value) |
| 27 | + }, |
| 28 | + (ipos_for_setting, ipos_for_probing) => { |
| 29 | + // lodash equal to compare maps and sets |
| 30 | + expect(_.isEqual(ipos_for_setting.myField, ipos_for_probing.myField)).toEqual(true) |
| 31 | + } |
| 32 | + ) |
| 33 | + }) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +describe('Update fields in the main process', () => |
| 38 | + createFieldsTest( |
| 39 | + async ( |
| 40 | + methods: ValueOf<typeof exampleChanges>, |
| 41 | + setValue: (ipos_for_setting: IPOS) => void, |
| 42 | + probeValue: (ipos_for_setting: IPOS, ipos_for_probing: IPOS) => void |
| 43 | + ) => { |
| 44 | + const {main_ipos, sub_ipos, sub_process} = await createConnectedInstances() |
| 45 | + setValue(main_ipos) |
| 46 | + |
| 47 | + for (const changeValue of methods) { |
| 48 | + changeValue(main_ipos.myField as any) |
| 49 | + probeValue(main_ipos, sub_ipos) |
| 50 | + } |
| 51 | + |
| 52 | + sub_process.destroy() |
| 53 | + }, |
| 54 | + (key) => `Update ${key}` |
| 55 | + ) |
| 56 | +) |
| 57 | + |
| 58 | +describe('Update fields in the subprocess', () => |
| 59 | + createFieldsTest( |
| 60 | + async ( |
| 61 | + methods: ValueOf<typeof exampleChanges>, |
| 62 | + setValue: (ipos_for_setting: IPOS) => void, |
| 63 | + probeValue: (ipos_for_setting: IPOS, ipos_for_probing: IPOS) => void |
| 64 | + ) => { |
| 65 | + const {main_ipos, sub_ipos, sub_process} = await createConnectedInstances() |
| 66 | + setValue(sub_ipos) |
| 67 | + |
| 68 | + for (const changeValue of methods) { |
| 69 | + changeValue(sub_ipos.myField as any) |
| 70 | + probeValue(sub_ipos, main_ipos) |
| 71 | + } |
| 72 | + |
| 73 | + sub_process.destroy() |
| 74 | + }, |
| 75 | + (key) => `Update ${key}` |
| 76 | + ) |
| 77 | +) |
0 commit comments