|
| 1 | +const CFError = require('cf-errors'); |
| 2 | +const Entity = require('../../logic/entities/Entity'); |
| 3 | +const output = require('./jsonArray.output'); |
| 4 | + |
| 5 | +describe('jsonArray output', () => { |
| 6 | + it('should return empty array for an empty array', () => { |
| 7 | + expect(output([])).toBe('[]'); |
| 8 | + }); |
| 9 | + |
| 10 | + it('should wrap single Entity into array', () => { |
| 11 | + const entity = new Entity(); |
| 12 | + entity.info = { id: '1', status: 'ok' }; |
| 13 | + |
| 14 | + const result = JSON.parse(output(entity)); |
| 15 | + expect(Array.isArray(result)).toBe(true); |
| 16 | + expect(result).toHaveLength(1); |
| 17 | + expect(result[0]).toEqual(entity.info); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should keep multiple Entities as array', () => { |
| 21 | + const entity1 = new Entity(); |
| 22 | + entity1.info = { id: '1' }; |
| 23 | + const entity2 = new Entity(); |
| 24 | + entity2.info = { id: '2' }; |
| 25 | + |
| 26 | + const result = JSON.parse(output([entity1, entity2])); |
| 27 | + expect(Array.isArray(result)).toBe(true); |
| 28 | + expect(result).toHaveLength(2); |
| 29 | + expect(result[0]).toEqual(entity1.info); |
| 30 | + expect(result[1]).toEqual(entity2.info); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should throw CFError if element is not an Entity', () => { |
| 34 | + expect(() => output({ id: '1' })).toThrow(CFError); |
| 35 | + expect(() => output([new Entity(), {}])).toThrow(CFError); |
| 36 | + }); |
| 37 | +}); |
0 commit comments