Skip to content

Commit

Permalink
✅ testing subscription shape value
Browse files Browse the repository at this point in the history
  • Loading branch information
chengcyber committed Aug 10, 2019
1 parent 8f321ca commit a56413e
Show file tree
Hide file tree
Showing 2 changed files with 435 additions and 0 deletions.
221 changes: 221 additions & 0 deletions src/filterFieldState.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,41 @@ describe('filterFieldState', () => {
})
}

const testShapeValueNotify = ({
key,
state,
newValue,
subscriptionValue,
shouldNotify = true
}) => {
it(`should ${shouldNotify ? '' : 'not'} notify when ${key} changes`, () => {
const result = filterFieldState({ ...state, [key]: newValue }, state, {
[key]: subscriptionValue
})
if (shouldNotify) {
expect(result).toEqual({
[key]: newValue,
name
})
} else {
expect(result).toBeUndefined()
}
})

it(`should notify when ${key} doesn't change, but is forced`, () => {
const result = filterFieldState(
state,
state,
{ [key]: subscriptionValue },
true
)
expect(result).toEqual({
[key]: state[key],
name
})
})
}

describe('filterFieldState.active', () => {
testValue('active', state, !state.active)
})
Expand Down Expand Up @@ -85,4 +120,190 @@ describe('filterFieldState', () => {
describe('filterFieldState.visited', () => {
testValue('visited', state, !state.visited)
})

describe('filterFieldState.value - shape value', () => {
const currentKey = 'value'
const currentValue = {
keyA: false,
keyB: { foo: false, bar: false },
keyC: undefined
}
const nextValueA = {
...currentValue,
keyA: true
}
const nextValueB = {
...currentValue,
keyB: { foo: true, bar: false }
}
const nextValueC = {
...currentValue,
keyC: null
}
const subscriptionValueA = {
keyA: true
}
const subscriptionValueB = {
keyB: true
}
const subscriptionValueC = {
keyC: true
}
const subscriptionValueFoo = {
keyB: { foo: true }
}
const subscriptionValueBar = {
keyB: { bar: true }
}
const subscriptionValueAll = {
keyA: true,
keyB: true,
keyC: true
}
const subscriptionValueWild = {
wildKey: {
whoAmI: true
}
}
describe('primitive value changes when subscription contains the key', () => {
testShapeValueNotify({
key: currentKey,
state: {
...state,
value: currentValue
},
newValue: nextValueA,
subscriptionValue: subscriptionValueA,
shouldNotify: true
})
testShapeValueNotify({
key: currentKey,
state: {
...state,
value: currentValue
},
newValue: nextValueA,
subscriptionValue: subscriptionValueAll,
shouldNotify: true
})
})
describe('primitive value does not change when subscription contains the key', () => {
testShapeValueNotify({
key: currentKey,
state: {
...state,
value: currentValue
},
newValue: currentValue,
subscriptionValue: subscriptionValueA,
shouldNotify: false
})
})
describe('primitive value changes when subscription does not contain the key', () => {
testShapeValueNotify({
key: currentKey,
state: {
...state,
value: currentValue
},
newValue: nextValueA,
subscriptionValue: subscriptionValueB,
shouldNotify: false
})
})
describe('object type value changes when subscription contains the key', () => {
testShapeValueNotify({
key: currentKey,
state: {
...state,
value: currentValue
},
newValue: nextValueB,
subscriptionValue: subscriptionValueB,
shouldNotify: true
})
testShapeValueNotify({
key: currentKey,
state: {
...state,
value: currentValue
},
newValue: nextValueB,
subscriptionValue: subscriptionValueAll,
shouldNotify: true
})
})
describe('object type value does not change when subscription contains the key', () => {
testShapeValueNotify({
key: currentKey,
state: {
...state,
value: currentValue
},
newValue: nextValueA,
subscriptionValue: subscriptionValueB,
shouldNotify: false
})
})
describe('object type value changes when subscription does not contain the key', () => {
testShapeValueNotify({
key: currentKey,
state: {
...state,
value: currentValue
},
newValue: nextValueB,
subscriptionValue: subscriptionValueA,
shouldNotify: false
})
})
describe('object type value partial changes when subscription contains the nested key', () => {
testShapeValueNotify({
key: currentKey,
state: {
...state,
value: currentValue
},
newValue: nextValueB,
subscriptionValue: subscriptionValueFoo,
shouldNotify: true
})
})
describe('object type value partial changes when subscription does not contain the nested key', () => {
testShapeValueNotify({
key: currentKey,
state: {
...state,
value: currentValue
},
newValue: nextValueB,
subscriptionValue: subscriptionValueBar,
shouldNotify: false
})
})
describe('undefined value changes to null value when subscription contains the key', () => {
testShapeValueNotify({
key: currentKey,
state: {
...state,
value: currentValue
},
newValue: nextValueC,
subscriptionValue: subscriptionValueC,
shouldNotify: true
})
})
describe('subscription contains a irrelative key', () => {
testShapeValueNotify({
key: currentKey,
state: {
...state,
value: currentValue
},
newValue: nextValueB,
subscriptionValue: subscriptionValueWild,
shouldNotify: false
})
})
})
})

0 comments on commit a56413e

Please sign in to comment.