/* eslint-env mocha */
/* eslint no-template-curly-in-string: 0 */
import assert from 'assert';
import { extractProp } from '../helper';
import getPropValue from '../../src/getPropValue';
describe('getPropValue', () => {
it('should export a function', () => {
const expected = 'function';
const actual = typeof getPropValue;
assert.equal(expected, actual);
});
it('should return undefined when not provided with a JSXAttribute', () => {
const expected = undefined;
const actual = getPropValue(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(() => {
getPropValue(prop);
}, Error);
});
describe('Null', () => {
it('should return true when no value is given', () => {
const prop = extractProp('
{ return "bar"; }} />');
const expected = 'function';
const actual = getPropValue(prop);
assert.equal(expected, typeof actual);
// For code coverage ¯\_(ツ)_/¯
actual();
});
});
describe('Function expression', () => {
it('should return a function', () => {
const prop = extractProp('
');
const expected = 'function';
const actual = getPropValue(prop);
assert.equal(expected, typeof actual);
// For code coverage ¯\_(ツ)_/¯
actual();
});
});
describe('Logical expression', () => {
it('should correctly infer result of && logical expression based on derived values', () => {
const prop = extractProp('
');
const expected = 'baz';
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
it('should return undefined when evaluating `undefined && undefined` ', () => {
const prop = extractProp('
');
const expected = undefined;
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
it('should correctly infer result of || logical expression based on derived values', () => {
const prop = extractProp('
');
const expected = 'bar';
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
it('should correctly infer result of || logical expression based on derived values', () => {
const prop = extractProp('
');
const expected = 'baz';
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
it('should return undefined when evaluating `undefined || undefined` ', () => {
const prop = extractProp('
');
const expected = undefined;
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
});
describe('Member expression', () => {
it('should return string representation of form `object.property`', () => {
const prop = extractProp('
');
const expected = 'bar.baz';
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
});
describe('Call expression', () => {
it('should return string representation of callee', () => {
const prop = extractProp('
');
const expected = 'bar';
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
it('should return string representation of callee', () => {
const prop = extractProp('
');
const expected = 'bar.call';
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
});
describe('Unary expression', () => {
it('should correctly evaluate an expression that prefixes with -', () => {
const prop = extractProp('
');
// -"bar" => NaN
const expected = true;
const actual = isNaN(getPropValue(prop));
assert.equal(expected, actual);
});
it('should correctly evaluate an expression that prefixes with -', () => {
const prop = extractProp('
');
const expected = -42;
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
it('should correctly evaluate an expression that prefixes with +', () => {
const prop = extractProp('
');
// +"bar" => NaN
const expected = true;
const actual = isNaN(getPropValue(prop));
assert.equal(expected, actual);
});
it('should correctly evaluate an expression that prefixes with +', () => {
const prop = extractProp('
');
const expected = 42;
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
it('should correctly evaluate an expression that prefixes with !', () => {
const prop = extractProp('
');
const expected = false; // !"bar" === false
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
it('should correctly evaluate an expression that prefixes with ~', () => {
const prop = extractProp('
');
const expected = -1; // ~"bar" === -1
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
it('should return true when evaluating `delete foo`', () => {
const prop = extractProp('
');
const expected = true;
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
it('should return undefined when evaluating `void foo`', () => {
const prop = extractProp('
');
const expected = undefined;
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
// TODO: We should fix this to check to see if we can evaluate it.
it('should return undefined when evaluating `typeof foo`', () => {
const prop = extractProp('
');
const expected = undefined;
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
});
describe('Update expression', () => {
it('should correctly evaluate an expression that prefixes with ++', () => {
const prop = extractProp('
');
// ++"bar" => NaN
const expected = true;
const actual = isNaN(getPropValue(prop));
assert.equal(expected, actual);
});
it('should correctly evaluate an expression that prefixes with --', () => {
const prop = extractProp('
');
const expected = true;
const actual = isNaN(getPropValue(prop));
assert.equal(expected, actual);
});
it('should correctly evaluate an expression that suffixes with ++', () => {
const prop = extractProp('
');
// "bar"++ => NaN
const expected = true;
const actual = isNaN(getPropValue(prop));
assert.equal(expected, actual);
});
it('should correctly evaluate an expression that suffixes with --', () => {
const prop = extractProp('
');
const expected = true;
const actual = isNaN(getPropValue(prop));
assert.equal(expected, actual);
});
});
describe('This expression', () => {
it('should return string value `this`', () => {
const prop = extractProp('
');
const expected = 'this';
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
});
describe('Conditional expression', () => {
it('should evaluate the conditional based on the derived values correctly', () => {
const prop = extractProp('
');
const expected = 'baz';
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
it('should evaluate the conditional based on the derived values correctly', () => {
const prop = extractProp('
');
const expected = 'bam';
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
it('should evaluate the conditional based on the derived values correctly', () => {
const prop = extractProp('
2) ? baz : bam} />');
const expected = 'bam';
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
});
describe('Binary expression', () => {
it('should evaluate the `==` operator correctly', () => {
const trueProp = extractProp('
');
const falseProp = extractProp('
');
const trueVal = getPropValue(trueProp);
const falseVal = getPropValue(falseProp);
assert.equal(true, trueVal);
assert.equal(false, falseVal);
});
it('should evaluate the `!=` operator correctly', () => {
const trueProp = extractProp('
');
const falseProp = extractProp('
');
const trueVal = getPropValue(trueProp);
const falseVal = getPropValue(falseProp);
assert.equal(true, trueVal);
assert.equal(false, falseVal);
});
it('should evaluate the `===` operator correctly', () => {
const trueProp = extractProp('
');
const falseProp = extractProp('
');
const trueVal = getPropValue(trueProp);
const falseVal = getPropValue(falseProp);
assert.equal(true, trueVal);
assert.equal(false, falseVal);
});
it('should evaluate the `!==` operator correctly', () => {
const trueProp = extractProp('
');
const falseProp = extractProp('
');
const trueVal = getPropValue(trueProp);
const falseVal = getPropValue(falseProp);
assert.equal(true, trueVal);
assert.equal(false, falseVal);
});
it('should evaluate the `<` operator correctly', () => {
const trueProp = extractProp('
');
const falseProp = extractProp('
');
const trueVal = getPropValue(trueProp);
const falseVal = getPropValue(falseProp);
assert.equal(true, trueVal);
assert.equal(false, falseVal);
});
it('should evaluate the `>` operator correctly', () => {
const trueProp = extractProp('
0} />');
const falseProp = extractProp('
2} />');
const trueVal = getPropValue(trueProp);
const falseVal = getPropValue(falseProp);
assert.equal(true, trueVal);
assert.equal(false, falseVal);
});
it('should evaluate the `<=` operator correctly', () => {
const trueProp = extractProp('
');
const falseProp = extractProp('
');
const trueVal = getPropValue(trueProp);
const falseVal = getPropValue(falseProp);
assert.equal(true, trueVal);
assert.equal(false, falseVal);
});
it('should evaluate the `>=` operator correctly', () => {
const trueProp = extractProp('
= 1} />');
const falseProp = extractProp('
= 2} />');
const trueVal = getPropValue(trueProp);
const falseVal = getPropValue(falseProp);
assert.equal(true, trueVal);
assert.equal(false, falseVal);
});
it('should evaluate the `<<` operator correctly', () => {
const prop = extractProp('
');
const expected = 4;
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
it('should evaluate the `>>` operator correctly', () => {
const prop = extractProp('
> 2} />');
const expected = 0;
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
it('should evaluate the `>>>` operator correctly', () => {
const prop = extractProp('
>> 1} />');
const expected = 1;
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
it('should evaluate the `+` operator correctly', () => {
const prop = extractProp('
');
const expected = 2;
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
it('should evaluate the `-` operator correctly', () => {
const prop = extractProp('
');
const expected = 0;
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
it('should evaluate the `*` operator correctly', () => {
const prop = extractProp('
');
const expected = 100;
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
it('should evaluate the `/` operator correctly', () => {
const prop = extractProp('
');
const expected = 5;
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
it('should evaluate the `%` operator correctly', () => {
const prop = extractProp('
');
const expected = 1;
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
it('should evaluate the `|` operator correctly', () => {
const prop = extractProp('
');
const expected = 11;
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
it('should evaluate the `^` operator correctly', () => {
const prop = extractProp('
');
const expected = 11;
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
it('should evaluate the `&` operator correctly', () => {
const prop = extractProp('
');
const expected = 0;
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
it('should evaluate the `in` operator correctly', () => {
const prop = extractProp('
');
const expected = false;
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
it('should evaluate the `instanceof` operator correctly', () => {
const prop = extractProp('
');
const expected = true;
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
it('should evaluate the `instanceof` operator when right side is not a function', () => {
const prop = extractProp('
');
const expected = false;
const actual = getPropValue(prop);
assert.equal(expected, actual);
});
});
describe('Object expression', () => {
it('should evaluate to a correct representation of the object in props', () => {
const prop = extractProp('
');
const expected = { bar: 'baz' };
const actual = getPropValue(prop);
assert.deepEqual(expected, actual);
});
});
describe('New expression', () => {
it('should return a new empty object', () => {
const prop = extractProp('
');
const expected = {};
const actual = getPropValue(prop);
assert.deepEqual(expected, actual);
});
});
describe('Array expression', () => {
it('should evaluate to correct representation of the the array in props', () => {
const prop = extractProp('
');
const expected = ['bar', 42, null];
const actual = getPropValue(prop);
assert.deepEqual(expected, actual);
});
});
it('should return an empty array provided an empty array in props', () => {
const prop = extractProp('
');
const expected = [];
const actual = getPropValue(prop);
assert.deepEqual(expected, actual);
});
});