Skip to content

Commit

Permalink
Merge d0bfcd0 into 9d2d3df
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromew committed Jul 2, 2020
2 parents 9d2d3df + d0bfcd0 commit 16bfbb7
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 4 deletions.
30 changes: 29 additions & 1 deletion src/node_modules/types/select.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 88 additions & 3 deletions test/types/select.js
Expand Up @@ -10,7 +10,9 @@ describe('select', () => {

const defaultType = {
decode: () => defaultValue,
encode() {},
encode: jest.fn().mockImplementation(() => {
defaultType.encode.bytes = defaultBytes;
}),
};

defaultType.decode.bytes = defaultBytes;
Expand All @@ -20,7 +22,9 @@ describe('select', () => {

const firstType = {
decode: () => firstValue,
encode() {},
encode: jest.fn().mockImplementation(() => {
firstType.encode.bytes = firstBytes;
}),
};

firstType.decode.bytes = firstBytes;
Expand All @@ -30,7 +34,9 @@ describe('select', () => {

const secondType = {
decode: () => secondValue,
encode() {},
encode: jest.fn().mockImplementation(() => {
secondType.encode.bytes = secondBytes;
}),
};

secondType.decode.bytes = secondBytes;
Expand Down Expand Up @@ -82,4 +88,83 @@ describe('select', () => {
expect(type.decode.bytes).toEqual(0);
expect(type[symbols.skip]).toEqual(true);
});

test('encode first option', () => {
jest.clearAllMocks();
const wstream = {};
const context = {};

const type = select(
when(() => true, firstType),
when(() => false, secondType),
defaultType
);

type.encode({}, wstream, context);

expect(type.encode.bytes).toEqual(firstBytes);
expect(type[symbols.skip]).toEqual(false);
expect(firstType.encode).toHaveBeenCalledTimes(1);
expect(secondType.encode).toHaveBeenCalledTimes(0);
expect(defaultType.encode).toHaveBeenCalledTimes(0);
});

test('encode second option', () => {
jest.clearAllMocks();
const wstream = {};
const context = {};

const type = select(
when(() => false, firstType),
when(() => true, secondType),
defaultType
);

type.encode({}, wstream, context);

expect(type.encode.bytes).toEqual(secondBytes);
expect(type[symbols.skip]).toEqual(false);
expect(firstType.encode).toHaveBeenCalledTimes(0);
expect(secondType.encode).toHaveBeenCalledTimes(1);
expect(defaultType.encode).toHaveBeenCalledTimes(0);
});

test('encode default option', () => {
jest.clearAllMocks();
const wstream = {};
const context = {};

const type = select(
when(() => false, firstType),
when(() => false, secondType),
defaultType
);

type.encode({}, wstream, context);

expect(type.encode.bytes).toEqual(defaultBytes);
expect(type[symbols.skip]).toEqual(false);
expect(firstType.encode).toHaveBeenCalledTimes(0);
expect(secondType.encode).toHaveBeenCalledTimes(0);
expect(defaultType.encode).toHaveBeenCalledTimes(1);
});

test('skip after encode', () => {
jest.clearAllMocks();
const wstream = {};
const context = {};

const type = select(
when(() => false, firstType),
when(() => false, secondType)
);

type.encode({}, wstream, context);

expect(type.encode.bytes).toEqual(0);
expect(type[symbols.skip]).toEqual(true);
expect(firstType.encode).toHaveBeenCalledTimes(0);
expect(secondType.encode).toHaveBeenCalledTimes(0);
expect(defaultType.encode).toHaveBeenCalledTimes(0);
});
});

0 comments on commit 16bfbb7

Please sign in to comment.