File size: 4,817 Bytes
1e92f2d |
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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
import { expect } from 'chai'
import { FlattenParams } from './flat.types.js'
import { set } from './set.js'
describe('module:flat.set', () => {
let params: FlattenParams
let newParams: FlattenParams
beforeEach(() => {
params = {
name: 'Wojtek',
surname: 'Krysiak',
age: 36,
'interest.OfMe.0': 'javascript',
'interest.OfMe.1': 'typescript',
'interest.OfMe.2': 'brainTumor',
interests: 'Generally everything',
'meta.position': 'CTO',
'meta.workingHours': '9:00-17:00',
'meta.duties': 'everything',
'meta.fun': '8/10',
initiallyNull: null,
}
})
it('sets regular property when it is default type', () => {
const age = 37
expect(set(params, 'age', age)).to.have.property('age', 37)
})
context('passing basic types', () => {
const newPropertyName = 'newProperty'
it('does not change the type when regular file is set', function () {
const file = new File([], 'amazing.me')
newParams = set(params, newPropertyName, file)
expect(newParams[newPropertyName]).to.equal(file)
})
it('does not change the type when Date is set', () => {
const date = new Date()
newParams = set(params, newPropertyName, date)
expect(newParams[newPropertyName]).to.equal(date)
})
it('sets null', () => {
expect(set(params, newPropertyName, null)).to.have.property(newPropertyName, null)
})
it('sets empty object', () => {
expect(set(params, newPropertyName, {})).to.deep.include({ [newPropertyName]: {} })
})
it('sets empty array', () => {
expect(set(params, newPropertyName, [])).to.deep.include({ [newPropertyName]: [] })
})
it('does nothing when setting undefined to some random key', () => {
expect(set(params, newPropertyName, undefined)).to.deep.equal(params)
})
})
context('passing array', () => {
const interest = ['js', 'ts']
beforeEach(() => {
newParams = set(params, 'interest.OfMe', interest)
})
it('replaces sets values for all new arrays items', () => {
expect(newParams).to.include({
'interest.OfMe.0': 'js',
'interest.OfMe.1': 'ts',
})
})
it('removes old values', () => {
expect(newParams).not.to.have.property('interest.OfMe.2')
})
it('leaves other values which name starts the same', () => {
expect(newParams).to.have.property('interests', params.interests)
})
})
context('value is undefined', () => {
const property = 'meta'
beforeEach(() => {
newParams = set(params, property)
})
it('removes all existing properties', () => {
expect(newParams).not.to.have.keys(
'meta.position',
'meta.workingHours',
'meta.duties',
'meta.fun',
)
})
it('does not set any new key', () => {
expect(Object.keys(newParams).length).to.eq(Object.keys(params).length - 4)
})
})
context('mixed type was inside and should be updated', () => {
const meta = {
position: 'adminJSCEO',
workingHours: '6:00-21:00',
}
beforeEach(() => {
newParams = set(params, 'meta', meta)
})
it('clears the previous value for nested string', () => {
expect(newParams).not.to.have.keys('meta.duties', 'meta.fun')
})
it('sets the new value for nested string', () => {
expect(newParams).to.include({
'meta.position': meta.position,
'meta.workingHours': meta.workingHours,
})
})
})
context('user wants to set nested property for already given root property', () => {
const newNestedNullValue = 'this is not null'
beforeEach(() => {
params = {
id: '6e264607-ad0b-4480-8e25-1bf54063465b',
title: 'Your new story',
status: 'draft',
postImage: null,
blogImageKeys: null,
blogImageMimeTypes: null,
blogImageBuckets: null,
blogImageSizes: null,
postUrl: 'your-new-story',
}
})
it('sets value for new nested property', () => {
const newNestedNullKey = 'blogImageKeys.nested'
newParams = set(params, newNestedNullKey, newNestedNullValue)
expect(newParams[newNestedNullKey]).to.eq(newNestedNullValue)
})
it('removes root property from keys', () => {
const newNestedNullKey = 'blogImageKeys.nested'
newParams = set(params, newNestedNullKey, newNestedNullValue)
expect(Object.keys(newParams)).not.to.include(newNestedNullKey.split('.')[0])
})
it('removes value from keys if new value is an array', () => {
const newNestedNullKey = 'blogImageKeys.0'
newParams = set(params, newNestedNullKey, newNestedNullValue)
expect(Object.keys(newParams)).not.to.include(newNestedNullKey.split('.')[0])
})
})
})
|