|
| 1 | +const { expect } = require('chai'); |
| 2 | +const { Fury } = require('@apielements/core'); |
| 3 | +const serializeText = require('../lib/serializeText'); |
| 4 | + |
| 5 | +const { minim: namespace } = new Fury(); |
| 6 | + |
| 7 | +const done = (error, result) => { |
| 8 | + if (error) { |
| 9 | + throw error; |
| 10 | + } else { |
| 11 | + return result; |
| 12 | + } |
| 13 | +}; |
| 14 | + |
| 15 | +describe('#serializeText', () => { |
| 16 | + it('can serialize a primitive element with value', () => { |
| 17 | + const stringElement = new namespace.elements.String('hello world'); |
| 18 | + const numberElement = new namespace.elements.Number(1); |
| 19 | + const booleanElement = new namespace.elements.Boolean(true); |
| 20 | + const nullElement = new namespace.elements.Null(); |
| 21 | + |
| 22 | + expect(serializeText(stringElement, done)).to.equal('hello world'); |
| 23 | + expect(serializeText(numberElement, done)).to.equal('1'); |
| 24 | + expect(serializeText(booleanElement, done)).to.equal('true'); |
| 25 | + expect(serializeText(nullElement, done)).to.equal('null'); |
| 26 | + }); |
| 27 | + |
| 28 | + it('can serialize an enum element with primitive values', () => { |
| 29 | + const enumElement = new namespace.elements.Enum(); |
| 30 | + enumElement.enumerations = ['north', 'east', 'south', 'west']; |
| 31 | + |
| 32 | + expect(serializeText(enumElement, done)).to.equal('north'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('can serialize a primitive element with default value', () => { |
| 36 | + const element = new namespace.elements.String(); |
| 37 | + element.attributes.set('default', 'hello world'); |
| 38 | + |
| 39 | + expect(serializeText(element, done)).to.equal('hello world'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('can serialize an element with references via parent tree', () => { |
| 43 | + const element = new namespace.elements.Element(); |
| 44 | + element.element = 'error'; |
| 45 | + |
| 46 | + const error = new namespace.elements.Element(); |
| 47 | + error.element = 'message'; |
| 48 | + error.id = 'error'; |
| 49 | + |
| 50 | + new namespace.elements.Category([ |
| 51 | + new namespace.elements.Category([ |
| 52 | + new namespace.elements.String('error message', { id: 'message' }), |
| 53 | + error, |
| 54 | + ], { classes: ['dataStructures'] }), |
| 55 | + new namespace.elements.Category([ |
| 56 | + element, |
| 57 | + ]), |
| 58 | + ]).freeze(); |
| 59 | + |
| 60 | + expect(serializeText(element, done)).to.equal('error message'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('can serialize a dataStructure element', () => { |
| 64 | + const element = new namespace.elements.DataStructure( |
| 65 | + new namespace.elements.String('hello world') |
| 66 | + ); |
| 67 | + |
| 68 | + expect(serializeText(element, done)).to.equal('hello world'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('can serialize from referenced element', () => { |
| 72 | + const element = new namespace.elements.Element(); |
| 73 | + element.element = 'ref'; |
| 74 | + element.content = 'message'; |
| 75 | + |
| 76 | + new namespace.elements.Category([ |
| 77 | + new namespace.elements.String('hello world', { id: 'message' }), |
| 78 | + element, |
| 79 | + ]).freeze(); |
| 80 | + |
| 81 | + expect(serializeText(element, done)).to.equal('hello world'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('errors with a non primitive element', () => { |
| 85 | + const objectElement = new namespace.elements.Object({ message: 'Hello world' }); |
| 86 | + const arrayElement = new namespace.elements.Array(['Hello', 'Doe']); |
| 87 | + const emptyEnumElement = new namespace.elements.Enum(); |
| 88 | + |
| 89 | + expect(() => serializeText(objectElement, done)).to.throw('Only primitive elements can be serialized as text/plain'); |
| 90 | + expect(() => serializeText(arrayElement, done)).to.throw('Only primitive elements can be serialized as text/plain'); |
| 91 | + expect(() => serializeText(emptyEnumElement, done)).to.throw('Only primitive elements can be serialized as text/plain'); |
| 92 | + }); |
| 93 | +}); |
0 commit comments