-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
113 lines (89 loc) · 2.51 KB
/
index.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const chai = require('chai')
const { describe, it, beforeEach } = require('mocha')
const sinon = require('sinon')
const { mutate, unpure } = require('.')
chai.use(require('sinon-chai'))
const { expect } = chai
const pluckName = ({ name }) => ({ name })
describe('@baethon/pure-mutation', () => {
let user
beforeEach(() => {
user = {
name: 'Jon',
lastname: 'Snow',
familyName: 'Stark'
}
})
describe('mutate()', () => {
it('mutates existing properties', () => {
mutate(user, (data) => ({
...data,
familyName: 'King of the north'
}))
expect(user).to.have.property('familyName', 'King of the north')
})
it('appends new keys', () => {
mutate(user, (data) => ({
...data,
king: true
}))
expect(user).to.have.property('king', true)
})
it('removes deleted keys', () => {
mutate(user, ({ familyName, ...data }) => data)
expect(user).not.to.have.property('familyName')
})
it('passes new ref to callback', () => {
const stub = sinon.stub().returns({ name: 'Jon' })
mutate(user, stub)
expect(stub).to.have.been.calledWith(sinon.match((data) => data !== user))
})
describe('mutatorOps', () => {
it('allows to pass assign fn', () => {
mutate(user, pluckName, {
assign: (inputRef, values) => {
inputRef.values = values
}
})
expect(user.values).to.eql({ name: 'Jon' })
})
it('allows to pass exclude fn', () => {
mutate(user, pluckName, {
exclude: (inputRef, keys) => {
inputRef.values = keys
}
})
expect(user.values).to.eql(['lastname', 'familyName'])
})
})
})
describe('unpure()', () => {
it('converts pure function to non-pure function', () => {
const fn = unpure(pluckName)
fn(user)
expect(user).to.eql({
name: 'Jon'
})
})
describe('mutatorOps', () => {
it('allows to pass assign fn', () => {
const fn = unpure(pluckName, {
assign: (inputRef, values) => {
inputRef.values = values
}
})
fn(user)
expect(user.values).to.eql({ name: 'Jon' })
})
it('allows to pass exclude fn', () => {
const fn = unpure(pluckName, {
exclude: (inputRef, keys) => {
inputRef.values = keys
}
})
fn(user)
expect(user.values).to.eql(['lastname', 'familyName'])
})
})
})
})