Skip to content

Commit 0d31680

Browse files
committed
add test for updating values #1
1 parent 6c76b48 commit 0d31680

File tree

5 files changed

+107
-4
lines changed

5 files changed

+107
-4
lines changed

src/__test__/deleteFields.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ describe('Deleting fields', () => {
1111

1212
sub_process.destroy()
1313
})
14+
1415
it('...in the sub process', async () => {
1516
const {main_ipos, sub_ipos, sub_process} = await createConnectedInstances()
1617

src/__test__/runCreateFieldsTest.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ import _ from 'lodash'
22
import IPOS from '../main'
33
import {examples} from './testData'
44

5+
type ValueOf<T> = T[keyof T]
6+
57
export default function createFieldsTest(
6-
initWith: (setValue: (ipos_for_setting: IPOS, reset?: boolean) => void, probeValue: (ipos_for_probing: IPOS) => void) => void,
8+
initWith: (
9+
setValue: (ipos_for_setting: IPOS, reset?: boolean) => void,
10+
probeValue: (ipos_for_probing: IPOS) => void
11+
) => Promise<void>,
712
createLabel: (key: string) => string
813
) {
914
function customizer(a: any, b: any) {
@@ -16,7 +21,7 @@ export default function createFieldsTest(
1621
for (const exampleKey in examples) {
1722
if (!examples.hasOwnProperty(exampleKey)) continue
1823
it(createLabel(exampleKey), async () => {
19-
const value: unknown = examples[exampleKey]
24+
const value: ValueOf<typeof examples> = examples[exampleKey as keyof typeof examples]
2025
await initWith(
2126
(ipos_for_setting, reset = false) => {
2227
if (!reset) {

src/__test__/subProcessIPCLoopback.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export default class subProcessIPCLoopback {
2-
private originalProcessSend;
2+
private readonly originalProcessSend;
33
private readonly originalProcessOn;
44
private subProcessListener?: NodeJS.MessageListener;
55
private mainProcessListener?: NodeJS.MessageListener;

src/__test__/testData.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const examples: { [key: string]: unknown } = {
1+
export const examples = {
22
'string': 'myString',
33
'number': 42,
44
'object': {
@@ -13,3 +13,23 @@ export const examples: { [key: string]: unknown } = {
1313
'set': new Set(['myItem', 42]),
1414
'function': (a: number, b: number) => a + b,
1515
}
16+
17+
// exemplary; don't iterate every possible method, just do one direct value assignment and one method
18+
export const exampleChanges = {
19+
array: [
20+
(array: typeof examples.array) => array[1] = 43,
21+
(array: typeof examples.array) => array.push('added value'),
22+
],
23+
object: [
24+
(object: typeof examples.object) => object.myKey = 'yourValue',
25+
// no mutating method
26+
],
27+
set: [
28+
// no direct value assignment
29+
(set: typeof examples.set) => set.add('added value'),
30+
],
31+
map: [
32+
// no direct value assignment
33+
(map: typeof examples.map) => map.set('myKey', 'yourValue'),
34+
],
35+
}

src/__test__/updateFields.test.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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

Comments
 (0)