Skip to content

Commit

Permalink
Fix test build
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Apr 21, 2019
1 parent 6730058 commit fd9b8b6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 83 deletions.
4 changes: 2 additions & 2 deletions test/channeldef.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ describe('fieldDef', () => {

describe('functionalTitleFormatter', () => {
it('should return correct title for argmin', () => {
expect(functionalTitleFormatter({field: 'f', aggregate: {argmin: 'a'}}, {})).toBe('f for argmin(a)');
expect(functionalTitleFormatter({field: 'f', aggregate: {argmin: 'a'}})).toBe('f for argmin(a)');
});
it('should return correct title for aggregate', () => {
expect(functionalTitleFormatter({field: 'f', aggregate: {argmax: 'a'}}, {})).toBe('f for argmax(a)');
expect(functionalTitleFormatter({field: 'f', aggregate: {argmax: 'a'}})).toBe('f for argmax(a)');
});
});

Expand Down
121 changes: 40 additions & 81 deletions test/compile/scale/type.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {rangeType, SCALE_CHANNELS, X, Y} from '../../../src/channel';
import {scaleType} from '../../../src/compile/scale/type';
import {defaultConfig} from '../../../src/config';
import * as log from '../../../src/log';
import {BAR, PRIMITIVE_MARKS, RULE} from '../../../src/mark';
import {ScaleType} from '../../../src/scale';
Expand All @@ -9,22 +8,18 @@ import {NOMINAL, ORDINAL} from '../../../src/type';
import * as util from '../../../src/util';
import {RECT} from './../../../src/mark';

const defaultScaleConfig = defaultConfig.scale;

describe('compile/scale', () => {
describe('type()', () => {
it('should return null for channel without scale', () => {
expect(scaleType({}, 'detail', {type: 'temporal', timeUnit: 'yearmonth'}, 'point', defaultScaleConfig)).toEqual(
null
);
expect(scaleType({}, 'detail', {type: 'temporal', timeUnit: 'yearmonth'}, 'point')).toEqual(null);
});

it(
'should show warning if users try to override the scale and use bin',
log.wrap(localLogger => {
expect(
scaleType({type: 'point'}, 'color', {type: 'quantitative', bin: true}, 'point', defaultScaleConfig)
).toEqual(ScaleType.BIN_ORDINAL);
expect(scaleType({type: 'point'}, 'color', {type: 'quantitative', bin: true}, 'point')).toEqual(
ScaleType.BIN_ORDINAL
);
expect(localLogger.warns[0]).toEqual(
log.message.scaleTypeNotWorkWithFieldDef(ScaleType.POINT, ScaleType.BIN_ORDINAL)
);
Expand All @@ -34,26 +29,24 @@ describe('compile/scale', () => {
describe('nominal/ordinal', () => {
describe('color', () => {
it('should return ordinal scale for nominal data by default.', () => {
expect(scaleType({}, 'color', {type: 'nominal'}, 'point', defaultScaleConfig)).toEqual(ScaleType.ORDINAL);
expect(scaleType({}, 'color', {type: 'nominal'}, 'point')).toEqual(ScaleType.ORDINAL);
});

it('should return ordinal scale for ordinal data.', () => {
expect(scaleType({}, 'color', {type: 'nominal'}, 'point', defaultScaleConfig)).toEqual(ScaleType.ORDINAL);
expect(scaleType({}, 'color', {type: 'nominal'}, 'point')).toEqual(ScaleType.ORDINAL);
});
});

describe('discrete channel (shape)', () => {
it('should return ordinal for nominal field', () => {
expect(scaleType({}, 'shape', {type: 'nominal'}, 'point', defaultScaleConfig)).toEqual(ScaleType.ORDINAL);
expect(scaleType({}, 'shape', {type: 'nominal'}, 'point')).toEqual(ScaleType.ORDINAL);
});

it(
'should return ordinal even if other type is specified',
log.wrap(localLogger => {
[ScaleType.LINEAR, ScaleType.BAND, ScaleType.POINT].forEach(badScaleType => {
expect(scaleType({type: badScaleType}, 'shape', {type: 'nominal'}, 'point', defaultScaleConfig)).toEqual(
ScaleType.ORDINAL
);
expect(scaleType({type: badScaleType}, 'shape', {type: 'nominal'}, 'point')).toEqual(ScaleType.ORDINAL);
const warns = localLogger.warns;
expect(warns[warns.length - 1]).toEqual(
log.message.scaleTypeNotWorkWithChannel('shape', badScaleType, 'ordinal')
Expand All @@ -65,7 +58,7 @@ describe('compile/scale', () => {
it(
'should return ordinal for an ordinal field and throw a warning.',
log.wrap(localLogger => {
expect(scaleType({}, 'shape', {type: 'ordinal'}, 'point', defaultScaleConfig)).toEqual(ScaleType.ORDINAL);
expect(scaleType({}, 'shape', {type: 'ordinal'}, 'point')).toEqual(ScaleType.ORDINAL);
expect(localLogger.warns[0]).toEqual(log.message.discreteChannelCannotEncode('shape', 'ordinal'));
})
);
Expand All @@ -80,7 +73,7 @@ describe('compile/scale', () => {

[ORDINAL, NOMINAL].forEach(t => {
[X, Y].forEach(channel => {
expect(scaleType({}, channel, {type: t}, mark, defaultScaleConfig)).toEqual(ScaleType.POINT);
expect(scaleType({}, channel, {type: t}, mark)).toEqual(ScaleType.POINT);
});
});
});
Expand All @@ -90,7 +83,7 @@ describe('compile/scale', () => {
[ORDINAL, NOMINAL].forEach(t => {
[X, Y].forEach(channel => {
[BAR, RULE, RECT].forEach(mark => {
expect(scaleType({}, channel, {type: t}, mark, defaultScaleConfig)).toEqual(ScaleType.BAND);
expect(scaleType({}, channel, {type: t}, mark)).toEqual(ScaleType.BAND);
});
});
});
Expand All @@ -99,7 +92,7 @@ describe('compile/scale', () => {
it('should return point scale for X,Y when mark is point', () => {
[ORDINAL, NOMINAL].forEach(t => {
[X, Y].forEach(channel => {
expect(scaleType({}, channel, {type: t}, 'point', defaultScaleConfig)).toEqual(ScaleType.POINT);
expect(scaleType({}, channel, {type: t}, 'point')).toEqual(ScaleType.POINT);
});
});
});
Expand All @@ -109,9 +102,7 @@ describe('compile/scale', () => {
log.wrap(localLogger => {
[ORDINAL, NOMINAL].forEach(t => {
[X, Y].forEach(channel => {
expect(scaleType({type: 'ordinal'}, channel, {type: t}, 'point', defaultScaleConfig)).toEqual(
ScaleType.POINT
);
expect(scaleType({type: 'ordinal'}, channel, {type: t}, 'point')).toEqual(ScaleType.POINT);
const warns = localLogger.warns;
expect(warns[warns.length - 1]).toEqual(
log.message.scaleTypeNotWorkWithChannel(channel, 'ordinal', 'point')
Expand All @@ -128,7 +119,7 @@ describe('compile/scale', () => {
PRIMITIVE_MARKS.forEach(mark => {
[ORDINAL, NOMINAL].forEach(t => {
OTHER_CONTINUOUS_CHANNELS.forEach(channel => {
expect(scaleType({}, channel, {type: t}, mark, defaultScaleConfig)).toEqual(ScaleType.POINT);
expect(scaleType({}, channel, {type: t}, mark)).toEqual(ScaleType.POINT);
});
});
});
Expand All @@ -138,128 +129,96 @@ describe('compile/scale', () => {

describe('temporal', () => {
it('should return time scale for temporal color field by default.', () => {
expect(scaleType({}, 'color', {type: 'temporal'}, 'point', defaultScaleConfig)).toBe('time');
expect(scaleType({}, 'color', {type: 'temporal'}, 'point')).toBe('time');
});

it(
'should return ordinal for temporal field and throw a warning.',
log.wrap(localLogger => {
expect(
scaleType({}, 'shape', {type: 'temporal', timeUnit: 'yearmonth'}, 'point', defaultScaleConfig)
).toEqual(ScaleType.ORDINAL);
expect(scaleType({}, 'shape', {type: 'temporal', timeUnit: 'yearmonth'}, 'point')).toEqual(ScaleType.ORDINAL);
expect(localLogger.warns[0]).toEqual(log.message.discreteChannelCannotEncode('shape', 'temporal'));
})
);

it('should return time for all time units.', () => {
for (const timeUnit of TIMEUNITS) {
expect(scaleType({}, Y, {type: 'temporal', timeUnit: timeUnit}, 'point', defaultScaleConfig)).toEqual(
ScaleType.TIME
);
expect(scaleType({}, Y, {type: 'temporal', timeUnit: timeUnit}, 'point')).toEqual(ScaleType.TIME);
}
});
});
describe('quantitative', () => {
it('should return linear scale for quantitative color field by default.', () => {
expect(scaleType({}, 'color', {type: 'quantitative'}, 'point', defaultScaleConfig)).toBe('linear');
expect(scaleType({}, 'color', {type: 'quantitative'}, 'point')).toBe('linear');
});

it('should return linear scale for piecewise quantitative color field by default.', () => {
expect(
scaleType(
{domain: [1, 2, 3], range: ['red', 'green', 'blue']},
'color',
{type: 'quantitative'},
'point',
defaultScaleConfig
)
scaleType({domain: [1, 2, 3], range: ['red', 'green', 'blue']}, 'color', {type: 'quantitative'}, 'point')
).toEqual(ScaleType.LINEAR);
});

it('should return ordinal bin scale for quantitative color field with binning.', () => {
expect(scaleType({}, 'color', {type: 'quantitative', bin: true}, 'point', defaultScaleConfig)).toEqual(
ScaleType.BIN_ORDINAL
);
expect(scaleType({}, 'color', {type: 'quantitative', bin: true}, 'point')).toEqual(ScaleType.BIN_ORDINAL);
});

it(
'should return ordinal for encoding quantitative field with a discrete channel and throw a warning.',
log.wrap(localLogger => {
expect(scaleType({}, 'shape', {type: 'quantitative'}, 'point', defaultScaleConfig)).toEqual(
ScaleType.ORDINAL
);
expect(scaleType({}, 'shape', {type: 'quantitative'}, 'point')).toEqual(ScaleType.ORDINAL);
expect(localLogger.warns[0]).toEqual(log.message.discreteChannelCannotEncode('shape', 'quantitative'));
})
);

it('should return linear scale for quantitative by default.', () => {
expect(scaleType({}, 'x', {type: 'quantitative'}, 'point', defaultScaleConfig)).toEqual(ScaleType.LINEAR);
expect(scaleType({}, 'x', {type: 'quantitative'}, 'point')).toEqual(ScaleType.LINEAR);
});

it('should return linear scale for quantitative even if binned.', () => {
expect(scaleType({}, 'opacity', {type: 'quantitative', bin: true}, 'point', defaultScaleConfig)).toEqual(
ScaleType.LINEAR
);
expect(scaleType({}, 'opacity', {type: 'quantitative', bin: true}, 'point')).toEqual(ScaleType.LINEAR);
});

it('should return linear scale for quantitative x and y.', () => {
expect(scaleType({}, 'x', {type: 'quantitative', bin: true}, 'point', defaultScaleConfig)).toEqual(
ScaleType.LINEAR
);
expect(scaleType({}, 'x', {type: 'quantitative', bin: true}, 'point')).toEqual(ScaleType.LINEAR);
});
});

describe('dataTypeMatchScaleType()', () => {
it('should return specified value if datatype is ordinal or nominal and specified scale type is the ordinal or nominal', () => {
expect(scaleType({type: ScaleType.ORDINAL}, 'shape', {type: 'ordinal'}, 'point', defaultScaleConfig)).toEqual(
ScaleType.ORDINAL
);
expect(scaleType({type: ScaleType.ORDINAL}, 'shape', {type: 'ordinal'}, 'point')).toEqual(ScaleType.ORDINAL);
});

it('should return default scale type if data type is temporal but specified scale type is not time or utc', () => {
expect(
scaleType({type: ScaleType.LINEAR}, 'x', {type: 'temporal', timeUnit: 'year'}, 'point', defaultScaleConfig)
).toEqual(ScaleType.TIME);
expect(scaleType({type: ScaleType.LINEAR}, 'x', {type: 'temporal', timeUnit: 'year'}, 'point')).toEqual(
ScaleType.TIME
);

expect(
scaleType(
{type: ScaleType.LINEAR},
'color',
{type: 'temporal', timeUnit: 'year'},
'point',
defaultScaleConfig
)
).toBe('time');
expect(scaleType({type: ScaleType.LINEAR}, 'color', {type: 'temporal', timeUnit: 'year'}, 'point')).toBe(
'time'
);
});

it('should return time if data type is temporal but specified scale type is discrete', () => {
expect(
scaleType({type: ScaleType.POINT}, 'x', {type: 'temporal', timeUnit: 'year'}, 'point', defaultScaleConfig)
).toEqual(ScaleType.TIME);
expect(scaleType({type: ScaleType.POINT}, 'x', {type: 'temporal', timeUnit: 'year'}, 'point')).toEqual(
ScaleType.TIME
);
});

it('should return default scale type if data type is temporal but specified scale type is time or utc or any discrete type', () => {
expect(
scaleType({type: ScaleType.LINEAR}, 'x', {type: 'temporal', timeUnit: 'year'}, 'point', defaultScaleConfig)
).toEqual(ScaleType.TIME);
expect(scaleType({type: ScaleType.LINEAR}, 'x', {type: 'temporal', timeUnit: 'year'}, 'point')).toEqual(
ScaleType.TIME
);
});

it('should return default scale type if data type is quantative but scale type do not support quantative', () => {
expect(scaleType({type: ScaleType.TIME}, 'color', {type: 'quantitative'}, 'point', defaultScaleConfig)).toEqual(
ScaleType.LINEAR
);
expect(scaleType({type: ScaleType.TIME}, 'color', {type: 'quantitative'}, 'point')).toEqual(ScaleType.LINEAR);
});

it('should return default scale type if data type is quantative and scale type supports quantative', () => {
expect(scaleType({type: ScaleType.TIME}, 'x', {type: 'quantitative'}, 'point', defaultScaleConfig)).toEqual(
ScaleType.LINEAR
);
expect(scaleType({type: ScaleType.TIME}, 'x', {type: 'quantitative'}, 'point')).toEqual(ScaleType.LINEAR);
});

it('should return default scale type if data type is quantative and scale type supports quantative', () => {
expect(scaleType({type: ScaleType.TIME}, 'x', {type: 'temporal'}, 'point', defaultScaleConfig)).toEqual(
ScaleType.TIME
);
expect(scaleType({type: ScaleType.TIME}, 'x', {type: 'temporal'}, 'point')).toEqual(ScaleType.TIME);
});
});
});
Expand Down

0 comments on commit fd9b8b6

Please sign in to comment.