-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutation.test.js
80 lines (79 loc) · 2.41 KB
/
mutation.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { mutationBuilder, _advancedQueryBuilder } from './index.js'
function createRemover (query) {
const f = _advancedQueryBuilder(query, { parent: true })
return function () {
const result = f(...arguments)
for (const [item, arr] of result) {
const index = arr.indexOf(item)
arr.splice(index, 1)
}
return arguments[0]
}
}
const personCreator = () => ({
age: 23,
name: 'Jesse Mitchell',
interests: ['programming', 'business'],
friends: [{
name: 'Bob',
age: 25,
interests: [{ type: 'fun' }]
}, {
name: 'Kevin',
age: 23,
interests: [{ type: 'fun' }, { type: 'see' }]
}, {
name: 'Steve',
age: 32
}, {
name: 'Aaron',
age: 17
}]
})
describe('Mutation Tests', () => {
test('Simple mutation (fixed)', () => {
const person = personCreator()
const f = mutationBuilder('$.age')
expect(f(person, 24)).toStrictEqual({ ...person, age: 24 })
})
test('Simple mutation (variable)', () => {
const person = personCreator()
const f = mutationBuilder('$.age')
expect(f(person, i => i + 1)).toStrictEqual({ ...person, age: 24 })
})
test('Complex nested mutation', () => {
const person = personCreator()
const f = mutationBuilder('$.friends.*.interests.*.type')
f(person, 'changed')
expect(person.friends.flatMap(i => i.interests).filter(i => i).map(i => i.type)).toStrictEqual(['changed', 'changed', 'changed'])
})
test('Complex mutation', () => {
const person = personCreator()
const f = mutationBuilder('$.interests.*')
expect(f(person, i => i + 1).interests).toStrictEqual(['programming1', 'business1'])
})
test('Removal mutation (using a query w/o context)', () => {
const person = personCreator()
const f = createRemover('$.friends.*{ "<": [{ "var": "age" }, 30] }')
expect(f(person).friends).toStrictEqual([{
name: 'Steve',
age: 32
}])
})
test('Removal mutation (using a query w/ context)', () => {
const person = personCreator()
const f = createRemover('$.friends.*{ "<": [{ "var": "age" }, { "context": "" }] }')
expect(f(person, 30).friends).toStrictEqual([{
name: 'Steve',
age: 32
}])
})
test('Removal mutation (using a JSONPath query w/ context)', () => {
const person = personCreator()
const f = createRemover('$.friends.[?(@.age < $)]')
expect(f(person, 30).friends).toStrictEqual([{
name: 'Steve',
age: 32
}])
})
})