|
| 1 | +import { Validator } from './object-validator.types' |
| 2 | +import { ObjectValidator } from './object-validator' |
| 3 | +import mainObjectValidator from '../' |
| 4 | + |
| 5 | +class PayloadBuilder { |
| 6 | + private payload = { |
| 7 | + status: 200, |
| 8 | + data: { |
| 9 | + serviceName: 'DatasetSP.save', |
| 10 | + error: null, |
| 11 | + status: '1', |
| 12 | + pendingPrinting: false, |
| 13 | + transactionId: '6788329C5763C2982A9537E6DD1D122D', |
| 14 | + responseBody: { |
| 15 | + total: 0, |
| 16 | + entities: { |
| 17 | + entity: { |
| 18 | + data: '' |
| 19 | + } |
| 20 | + } |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + static aPayload() { |
| 26 | + return new PayloadBuilder() |
| 27 | + } |
| 28 | + |
| 29 | + private reset() { |
| 30 | + this.payload = {} as any |
| 31 | + } |
| 32 | + |
| 33 | + setStatus(s: any) { |
| 34 | + this.payload.status = s |
| 35 | + return this |
| 36 | + } |
| 37 | + |
| 38 | + setData(key: any, value: any) { |
| 39 | + this.payload.data[key] = value |
| 40 | + return this |
| 41 | + } |
| 42 | + |
| 43 | + build() { |
| 44 | + const payload = this.payload |
| 45 | + this.reset() |
| 46 | + return payload |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +describe('ObjectValidator', () => { |
| 51 | + let sut: Validator |
| 52 | + let payload: PayloadBuilder |
| 53 | + |
| 54 | + beforeEach(() => { |
| 55 | + sut = new ObjectValidator() |
| 56 | + payload = PayloadBuilder.aPayload() |
| 57 | + }) |
| 58 | + |
| 59 | + test('should be valid if fields match with provided payload entries', () => { |
| 60 | + const p = payload.build() |
| 61 | + |
| 62 | + const isValid = sut.isValid(p, [ |
| 63 | + ['status'], |
| 64 | + ['data.status'], |
| 65 | + ['data.responseBody.entities.entity'] |
| 66 | + ]) |
| 67 | + |
| 68 | + expect(isValid).toBeTruthy() |
| 69 | + }) |
| 70 | + |
| 71 | + test('should be valid if fields are repeated and match with provided payload entries', () => { |
| 72 | + const p = payload.build() |
| 73 | + |
| 74 | + const isValid = sut.isValid(p, [ |
| 75 | + ['data.status'], |
| 76 | + ['data.status'], |
| 77 | + ['data.status'] |
| 78 | + ]) |
| 79 | + |
| 80 | + expect(isValid).toBeTruthy() |
| 81 | + }) |
| 82 | + |
| 83 | + test('should be invalid if fields not exists in provided payload', () => { |
| 84 | + const p = payload.build() |
| 85 | + const isValid = sut.isValid(p, [['invalid'], ['invalid.field']]) |
| 86 | + expect(isValid).toBeFalsy() |
| 87 | + }) |
| 88 | + |
| 89 | + test('should be invalid if fields is not provided', () => { |
| 90 | + const p = payload.build() |
| 91 | + const isValid = sut.isValid(p, []) |
| 92 | + expect(isValid).toBeFalsy() |
| 93 | + }) |
| 94 | + |
| 95 | + test('should be invalid if not provided payload', () => { |
| 96 | + const isValid = sut.isValid({}, [['status']]) |
| 97 | + expect(isValid).toBeFalsy() |
| 98 | + }) |
| 99 | + |
| 100 | + test('should be invalid if one of many fields not exists in provided payload', () => { |
| 101 | + const p = payload.build() |
| 102 | + |
| 103 | + const isValid = sut.isValid(p, [ |
| 104 | + ['status'], |
| 105 | + ['data.responseBody.total'], |
| 106 | + ['ivalid.field'] |
| 107 | + ]) |
| 108 | + |
| 109 | + expect(isValid).toBeFalsy() |
| 110 | + }) |
| 111 | + |
| 112 | + test('should calls isValid method with correct params', () => { |
| 113 | + const spy = jest.spyOn(sut, 'isValid') |
| 114 | + const p = payload.build() |
| 115 | + |
| 116 | + sut.isValid(p, [['status'], ['data.status'], ['data.responseBody.total']]) |
| 117 | + |
| 118 | + expect(spy).toHaveBeenCalledTimes(4) |
| 119 | + expect(spy).toBeCalledWith(p, [ |
| 120 | + ['status'], |
| 121 | + ['data.status'], |
| 122 | + ['data.responseBody.total'] |
| 123 | + ]) |
| 124 | + }) |
| 125 | + |
| 126 | + test('should be valid if fields and values match with provided payload', () => { |
| 127 | + const p = payload |
| 128 | + .setStatus(null) |
| 129 | + .setData('error', false) |
| 130 | + .setData('status', ' ') |
| 131 | + .build() |
| 132 | + |
| 133 | + const isValid = sut.isValid(p, [ |
| 134 | + ['status', null], |
| 135 | + ['data.status', ' '], |
| 136 | + ['data.error', false], |
| 137 | + ['data.responseBody.total', 0], |
| 138 | + ['data.responseBody.entities.entity.data', ''] |
| 139 | + ]) |
| 140 | + |
| 141 | + expect(isValid).toBeTruthy() |
| 142 | + }) |
| 143 | + |
| 144 | + test('should be invalid if fields and values not match with provided payload', () => { |
| 145 | + const p = payload.setStatus(400).build() |
| 146 | + const isValid = sut.isValid(p, [['status', 200]]) |
| 147 | + expect(isValid).toBeFalsy() |
| 148 | + }) |
| 149 | + |
| 150 | + test('should be invalid if one of many fields and values not match with provided payload', () => { |
| 151 | + const p = payload |
| 152 | + .setData('serviceName', 'any service') |
| 153 | + .setData('status', '1') |
| 154 | + .setStatus(200) |
| 155 | + .build() |
| 156 | + |
| 157 | + const isValid = sut.isValid(p, [ |
| 158 | + ['status', 200], |
| 159 | + ['data.status', '1'], |
| 160 | + ['data.serviceName', 'invalid'] |
| 161 | + ]) |
| 162 | + |
| 163 | + expect(isValid).toBeFalsy() |
| 164 | + }) |
| 165 | + |
| 166 | + test('should be invalid if value is null and provided payload value is empty string', () => { |
| 167 | + const p = payload.setStatus('').build() |
| 168 | + const isValid = sut.isValid(p, [['status', null]]) |
| 169 | + expect(isValid).toBeFalsy() |
| 170 | + }) |
| 171 | + |
| 172 | + test('should be defined', () => { |
| 173 | + expect(mainObjectValidator).toBeDefined() |
| 174 | + expect(mainObjectValidator).toEqual(sut) |
| 175 | + expect(mainObjectValidator.isValid({}, [])).toBeFalsy() |
| 176 | + expect( |
| 177 | + mainObjectValidator.isValid({ foo: 'bar' }, [['foo', 'bar']]) |
| 178 | + ).toBeTruthy() |
| 179 | + }) |
| 180 | +}) |
0 commit comments