Skip to content

Commit

Permalink
fix(RandomTableResultSet): Fix bad unserialization of DisplayOptions
Browse files Browse the repository at this point in the history
Closes #52
  • Loading branch information
derikb committed Jan 8, 2022
1 parent 8272922 commit 35eeed2
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/RandomTableResultSet.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { capitalize, defaultToJSON } from './r_helpers.js';
import RandomTableResult from './RandomTableResult.js';
import TableErrorResult from './TableErrorResult.js';
import DisplayOptions from './DisplayOptions.js';

/**
* Set of table results.
Expand All @@ -9,7 +10,7 @@ export default class RandomTableResultSet {
/**
* @property {String} title Title from the RandomTable parent
* @property {RandomTableResult[]} results Randomized results.
* @property {Map[DisplayOptions]} displayOptions Display settings from the RandomTable parent.
* @property {Map[DisplayOptions]|Object} displayOptions Display settings from the RandomTable parent.
*/
constructor ({
title = '',
Expand All @@ -25,6 +26,17 @@ export default class RandomTableResultSet {
this.displayOptions = displayOptions;
} else {
this.displayOptions = new Map();
Object.keys(displayOptions).forEach((key) => {
const data = displayOptions[key];
const tableName = data.table || '';
if (tableName) {
if (data instanceof DisplayOptions) {
this.displayOptions.set(tableName, data);
return;
}
this.displayOptions.set(tableName, new DisplayOptions(data));
}
});
}
}
/**
Expand Down
46 changes: 46 additions & 0 deletions test/DisplayOptionsTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

import { expect } from 'chai';

import DisplayOptions from '../src/DisplayOptions.js';

describe('DisplayOptions', function () {
it('should serialize the class to an object (true options)', function () {
const opts = new DisplayOptions({
table: 'colors',
hide_table: true,
hide_result: 1,
hide_desc: '1'
});

expect(opts.hide_table).to.be.true;
expect(opts.hide_result).to.be.true;
expect(opts.hide_desc).to.be.true;

expect(opts.toJSON()).to.deep.equal({
table: 'colors',
hide_table: true,
hide_result: true,
hide_desc: true,
className: 'DisplayOptions'
});
});

it('should serialize the class to an object (false options)', function () {
const opts = new DisplayOptions({
table: 'colors',
hide_table: false,
hide_result: 0,
hide_desc: 'no'
});

expect(opts.hide_table).to.be.false;
expect(opts.hide_result).to.be.false;
expect(opts.hide_desc).to.be.false;

expect(opts.toJSON()).to.deep.equal({
table: 'colors',
className: 'DisplayOptions'
});
});
});
47 changes: 47 additions & 0 deletions test/RandomTableResultSetTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,55 @@ import { expect } from 'chai';
import DisplayOptions from '../src/DisplayOptions.js';
import RandomTableResult from '../src/RandomTableResult.js';
import RandomTableResultSet from '../src/RandomTableResultSet.js';
import TableErrorResult from '../src/TableErrorResult.js';

describe('RandomTableResultSet', function () {
it('should unserialize an object to the class props', function () {
const result1 = {
table: 'default',
result: 'blue'
};
const result2 = {
table: 'Error',
result: 'Ran out of room',
className: 'TableErrorResult'
};
const result3 = {
table: 'hue',
result: 'Cerulean',
desc: 'in some place',
className: 'RandomTableResult'
};
const displayOptionsObj = {
default: {
table: 'default',
hide_table: true,
className: 'DisplayOptions'
},
hue: new DisplayOptions({
table: 'hue',
hide_table: false
})
};

const resultSet = new RandomTableResultSet({
title: 'colors',
results: [
result1,
result2,
result3
],
displayOptions: displayOptionsObj
});

expect(resultSet.title).to.equal('colors');
expect(resultSet.results[0]).to.be.instanceOf(RandomTableResult);
expect(resultSet.results[1]).to.be.instanceOf(TableErrorResult);
expect(resultSet.results[2]).to.be.instanceOf(RandomTableResult);
expect(resultSet.displayOptions.get('default')).to.be.instanceOf(DisplayOptions);
expect(resultSet.displayOptions.get('hue')).to.be.instanceOf(DisplayOptions);
});

it('should output a string from the set with no display options', function () {
const result1 = new RandomTableResult({
table: 'default',
Expand Down

0 comments on commit 35eeed2

Please sign in to comment.