Skip to content

Commit

Permalink
test: collect
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanhaticus committed Nov 10, 2024
1 parent c214279 commit eef3b01
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions __tests__/serializers/utils/collect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { Chance } from 'chance';
import { assertMetadataIsPresent } from '../../../src/serializers/utils/assertMetadataIsPresent';
import { collect } from '../../../src/serializers/utils/collect';
import { getMetadataBySymbol } from '../../../src/serializers/utils/getMetadataBySymbol';

jest.mock('../../../src/serializers/utils/assertMetadataIsPresent');
const assertMetadataIsPresentMocked = jest.mocked(assertMetadataIsPresent);

jest.mock('../../../src/serializers/utils/getMetadataBySymbol');
const getMetadataBySymbolMocked = jest.mocked(getMetadataBySymbol);

describe('`collect`', () => {
let chance: Chance.Chance;

beforeEach(() => {
chance = new Chance();
});

it('should assert metadata is present in the given object', () => {
const object = {
a: chance.string(),
};

collect(object, Symbol());

expect(assertMetadataIsPresentMocked).toHaveBeenCalledTimes(1);
expect(assertMetadataIsPresentMocked).toHaveBeenCalledWith(object);
});

it('should get metadata by symbol', () => {
const symbol = Symbol(chance.string());
const object = {
a: chance.string(),
};

getMetadataBySymbolMocked.mockReturnValueOnce(symbol);

collect(object, symbol);

expect(getMetadataBySymbolMocked).toHaveBeenCalledTimes(1);
expect(getMetadataBySymbolMocked).toHaveBeenCalledWith(object, symbol);
});

describe('when the metadata is an array of keys', () => {
it('should return an empty object when the array is empty', () => {
const object = {
a: chance.string(),
};

getMetadataBySymbolMocked.mockReturnValueOnce([]);

const result = collect(object, Symbol());

expect(result).toEqual({});
});

it('should return an object with the keys and values from the object', () => {
const key1 = chance.string();
const key2 = chance.string();
const value1 = chance.string();
const value2 = chance.string();

const object = {
[key1]: value1,
[key2]: value2,
};

getMetadataBySymbolMocked.mockReturnValueOnce([key1, key2]);

const result = collect(object, Symbol());

expect(result).toEqual({
[key1]: value1,
[key2]: value2,
});
});

it('should ignore keys that are not present in the object', () => {
const key1 = chance.string();
const key2 = chance.string();
const value1 = chance.string();

const object = {
[key1]: value1,
};

getMetadataBySymbolMocked.mockReturnValueOnce([key1, key2]);

const result = collect(object, Symbol());

expect(result).toEqual({
[key1]: value1,
});
});
});

describe('when the metadata is a key', () => {
it('should return the value from the object', () => {
const key = chance.string();
const value = chance.string();

const object = {
[key]: value,
};

getMetadataBySymbolMocked.mockReturnValueOnce(key);

const result = collect(object, Symbol());

expect(result).toBe(value);
});
});
});

0 comments on commit eef3b01

Please sign in to comment.