Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

Commit

Permalink
test: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kristw committed Oct 2, 2019
1 parent 1f93567 commit 4b40c84
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 9 deletions.
17 changes: 8 additions & 9 deletions packages/superset-ui-encodable/src/encoders/Encoder.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { flatMap } from 'lodash';
import { ChannelDef } from '../types/ChannelDef';
import { ChannelDef, TypedFieldDef } from '../types/ChannelDef';
import { MayBeArray } from '../types/Base';
import { isFieldDef } from '../typeGuards/ChannelDef';
import { isArray, isNotArray } from '../typeGuards/Base';
import { isNotArray } from '../typeGuards/Base';
import ChannelEncoder from './ChannelEncoder';
import {
EncodingConfig,
Expand Down Expand Up @@ -35,8 +35,8 @@ export default class Encoder<Config extends EncodingConfig> {
const channels: { [k in keyof Config]?: MayBeArray<ChannelEncoder<ChannelDef>> } = {};

channelNames.forEach(name => {
const channelEncoding = encoding[name];
if (isArray(channelEncoding)) {
const channelEncoding = encoding[name] as MayBeArray<ChannelDef>;
if (Array.isArray(channelEncoding)) {
const definitions = channelEncoding;
channels[name] = definitions.map(
(definition, i) =>
Expand All @@ -46,7 +46,7 @@ export default class Encoder<Config extends EncodingConfig> {
name: `${name}[${i}]`,
}),
);
} else if (isNotArray(channelEncoding)) {
} else {
const definition = channelEncoding;
channels[name] = new ChannelEncoder({
channelType: channelTypes[name],
Expand Down Expand Up @@ -80,15 +80,14 @@ export default class Encoder<Config extends EncodingConfig> {
return Object.keys(this.channelTypes) as (keyof Config)[];
}

getChannelsAsArray() {
getChannelEncoders() {
return this.getChannelNames().map(name => this.channels[name]);
}

getGroupBys() {
const fields = flatMap(this.getChannelsAsArray())
const fields = flatMap(this.getChannelEncoders())
.filter(c => c.isGroupBy())
.map(c => (isFieldDef(c.definition) ? c.definition.field : ''))
.filter(field => field !== '');
.map(c => (c.definition as TypedFieldDef).field!);

return Array.from(new Set(fields));
}
Expand Down
64 changes: 64 additions & 0 deletions packages/superset-ui-encodable/test/encoders/Encoder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import createEncoderFactory from '../../src/encoders/createEncoderFactory';

describe('Encoder', () => {
const factory = createEncoderFactory<{
x: ['X', number];
y: ['Y', number];
color: ['Color', string];
shape: ['Category', string];
tooltip: ['Text', string, 'multiple'];
}>({
channelTypes: {
x: 'X',
y: 'Y',
color: 'Color',
shape: 'Category',
tooltip: 'Text',
},
defaultEncoding: {
x: { type: 'quantitative', field: 'speed' },
y: { type: 'quantitative', field: 'price' },
color: { type: 'nominal', field: 'brand' },
shape: { type: 'nominal', field: 'brand' },
tooltip: [{ field: 'make' }, { field: 'model' }],
},
});

const encoder = factory.create();

describe('new Encoder()', () => {
it('creates new encoder', () => {
expect(encoder).toBeDefined();
});
});
describe('.getChannelNames()', () => {
it('returns an array of channel names', () => {
expect(encoder.getChannelNames()).toEqual(['x', 'y', 'color', 'shape', 'tooltip']);
});
});
describe('.getChannelEncoders()', () => {
it('returns an array of channel encoders', () => {
expect(encoder.getChannelEncoders()).toHaveLength(5);
});
});
describe('.getGroupBys()', () => {
it('returns an array of groupby fields', () => {
expect(encoder.getGroupBys()).toEqual(['brand', 'make', 'model']);
});
});
describe('.hasLegend()', () => {
it('returns true if has legend', () => {
expect(encoder.hasLegend()).toBeTruthy();
});
it('returns false if does not have legend', () => {
expect(
factory
.create({
color: { type: 'nominal', field: 'brand', legend: false },
shape: { value: 'diamond' },
})
.hasLegend(),
).toBeFalsy();
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import createEncoderFactory from '../../src/encoders/createEncoderFactory';

describe('createEncoderFactory()', () => {
it('supports defaultEncoding as fixed value', () => {
const factory = createEncoderFactory<{
x: ['X', number];
}>({
channelTypes: {
x: 'X',
},
defaultEncoding: {
x: { type: 'quantitative', field: 'speed' },
},
});

const encoder = factory.create();
expect(encoder.encoding).toEqual({
x: { type: 'quantitative', field: 'speed' },
});
});
it('supports completeEncoding for customization', () => {
const factory = createEncoderFactory<{
color: ['Color', string];
}>({
channelTypes: {
color: 'Color',
},
completeEncoding: () => ({
color: { value: 'red' },
}),
});

const encoder = factory.create();
expect(encoder.encoding).toEqual({
color: { value: 'red' },
});
});
});

0 comments on commit 4b40c84

Please sign in to comment.