/* eslint-env mocha */ /* eslint no-template-curly-in-string: 0 */ import assert from 'assert'; import { extractProp } from '../helper'; import { getLiteralPropValue } from '../../src/getPropValue'; describe('getLiteralPropValue', () => { it('should export a function', () => { const expected = 'function'; const actual = typeof getLiteralPropValue; assert.equal(expected, actual); }); it('should return undefined when not provided with a JSXAttribute', () => { const expected = undefined; const actual = getLiteralPropValue(1); assert.equal(expected, actual); }); it('should throw error when trying to get value from unknown node type', () => { const prop = { type: 'JSXAttribute', value: { type: 'JSXExpressionContainer', }, }; assert.throws(() => { getLiteralPropValue(prop); }, Error); }); describe('Null', () => { it('should return true when no value is given', () => { const prop = extractProp('
'); const expected = true; const actual = getLiteralPropValue(prop); assert.equal(expected, actual); }); }); describe('Literal', () => { it('should return correct string if value is a string', () => { const prop = extractProp(''); const expected = 'bar'; const actual = getLiteralPropValue(prop); assert.equal(expected, actual); }); it('should return correct string if value is a string expression', () => { const prop = extractProp(''); const expected = 'bar'; const actual = getLiteralPropValue(prop); assert.equal(expected, actual); }); it('should return correct integer if value is a integer expression', () => { const prop = extractProp(''); const expected = 1; const actual = getLiteralPropValue(prop); assert.equal(expected, actual); }); it('should convert "true" to boolean type', () => { const prop = extractProp(''); const expected = true; const actual = getLiteralPropValue(prop); assert.equal(expected, actual); }); it('should convert "TrUE" to boolean type', () => { const prop = extractProp(''); const expected = true; const actual = getLiteralPropValue(prop); assert.equal(expected, actual); }); it('should convert "false" to boolean type', () => { const prop = extractProp(''); const expected = false; const actual = getLiteralPropValue(prop); assert.equal(expected, actual); }); it('should convert "FaLsE" to boolean type', () => { const prop = extractProp(''); const expected = false; const actual = getLiteralPropValue(prop); assert.equal(expected, actual); }); it('should return String null when value is null', () => { const prop = extractProp(''); const expected = 'null'; const actual = getLiteralPropValue(prop); assert.equal(expected, actual); }); }); describe('JSXElement', () => { it('should return null', () => { const prop = extractProp(' />'); const expected = null; const actual = getLiteralPropValue(prop); assert.equal(expected, actual); }); }); describe('Identifier', () => { it('should return null', () => { const prop = extractProp(''); const expected = null; const actual = getLiteralPropValue(prop); assert.equal(expected, actual); }); it('should return undefined when identifier is literally `undefined`', () => { const prop = extractProp(''); const expected = undefined; const actual = getLiteralPropValue(prop); assert.equal(expected, actual); }); }); describe('Template literal', () => { it('should return template literal with vars wrapped in curly braces', () => { const prop = extractProp(''); const expected = 'bar {baz}'; const actual = getLiteralPropValue(prop); assert.equal(expected, actual); }); it('should drop variables in template literals that are literally undefined', () => { const prop = extractProp(''); const expected = 'bar '; const actual = getLiteralPropValue(prop); assert.equal(expected, actual); }); }); describe('Tagged Template literal', () => { it('should return template literal with vars wrapped in curly braces', () => { const prop = extractProp(''); const expected = 'bar {baz}'; const actual = getLiteralPropValue(prop); assert.equal(expected, actual); }); it('should drop variables in template literals that are literally undefined', () => { const prop = extractProp(''); const expected = 'bar '; const actual = getLiteralPropValue(prop); assert.equal(expected, actual); }); }); describe('Arrow function expression', () => { it('should return null', () => { const prop = extractProp('