Skip to content

Commit

Permalink
refactor: callsToWrapUnions
Browse files Browse the repository at this point in the history
  • Loading branch information
joscha committed Sep 28, 2024
1 parent f680244 commit 5ecec5e
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions test/test_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -3522,7 +3522,9 @@ suite('types', () => {
};
const animalTypes = [Dog, Cat];

let callsToWrapUnions = 0;
const wrapUnions = (types) => {
callsToWrapUnions++;
assert.deepEqual(types.map(t => t.name), ['Dog', 'Cat']);
return (animal) => {
const animalType = ((animal) => {
Expand All @@ -3537,19 +3539,10 @@ suite('types', () => {
}
};

// TODO: replace this with a mock when available
// currently we're on mocha without sinon
function mockWrapUnions() {
mockWrapUnions.calls = typeof mockWrapUnions.calls === 'undefined'
? 1
: ++mockWrapUnions.calls;
return wrapUnions.apply(null, arguments);
}

// Ambiguous, but we have a projection function
const Animal = Type.forSchema(animalTypes, { wrapUnions: mockWrapUnions });
const Animal = Type.forSchema(animalTypes, { wrapUnions });
Animal.toBuffer({ meow: '🐈' });
assert.equal(mockWrapUnions.calls, 1);
assert.equal(callsToWrapUnions, 1);
assert.throws(() => Animal.toBuffer({ snap: '🐊' }), /Unknown animal/)
});

Expand Down

0 comments on commit 5ecec5e

Please sign in to comment.