| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- import { DECODED, ENCODED, MockIdStrategy } from '../../config/config.service.mock';
- import { IdCodec } from './id-codec';
- describe('IdCodecService', () => {
- let idCodec: IdCodec;
- beforeEach(async () => {
- idCodec = new IdCodec(new MockIdStrategy());
- });
- describe('encode()', () => {
- it('works with a string', () => {
- const input = 'id';
- const result = idCodec.encode(input);
- expect(result).toEqual(ENCODED);
- });
- it('works with a number', () => {
- const input = 123;
- const result = idCodec.encode(input);
- expect(result).toEqual(ENCODED);
- });
- it('works with a boolean', () => {
- const input = true;
- const result = idCodec.encode(input);
- expect(result).toEqual(true);
- });
- it('passes through null or undefined without throwing', () => {
- expect(idCodec.encode(null as any)).toBeNull();
- expect(idCodec.encode(undefined as any)).toBeUndefined();
- });
- it('returns a clone of the input object', () => {
- const input = { id: 'id', name: 'foo' };
- const result = idCodec.encode(input);
- expect(result).not.toBe(input);
- });
- it('returns a deep clone', () => {
- const obj1 = { 1: true };
- const obj2 = { 2: true };
- const arr = [obj1, obj2];
- const parent = { myArray: arr };
- const input = { foo: parent };
- const result = idCodec.encode(input);
- expect(result).not.toBe(input);
- expect(result.foo).not.toBe(parent);
- expect(result.foo.myArray).not.toBe(arr);
- expect(result.foo.myArray[0]).not.toBe(obj1);
- expect(result.foo.myArray[1]).not.toBe(obj2);
- });
- it('does not clone complex object instances', () => {
- // tslint:disable:no-floating-promises
- const promise = new Promise(() => {
- /**/
- });
- const date = new Date();
- const regex = new RegExp('');
- const input = {
- promise,
- date,
- regex,
- };
- const result = idCodec.encode(input);
- expect(result.promise).toBe(promise);
- expect(result.date).toBe(date);
- expect(result.regex).toBe(regex);
- // tslint:enable:no-floating-promises
- });
- it('works with simple entity', () => {
- const input = { id: 'id', name: 'foo' };
- const result = idCodec.encode(input);
- expect(result).toEqual({ id: ENCODED, name: 'foo' });
- });
- it('works with 2-level nested entities', () => {
- const input = {
- id: 'id',
- friend: { id: 'id' },
- };
- const result = idCodec.encode(input);
- expect(result).toEqual({
- id: ENCODED,
- friend: { id: ENCODED },
- });
- });
- it('works with 3-level nested entities', () => {
- const input = {
- id: 'id',
- friend: {
- dog: { id: 'id' },
- },
- };
- const result = idCodec.encode(input);
- expect(result).toEqual({
- id: ENCODED,
- friend: {
- dog: { id: ENCODED },
- },
- });
- });
- it('works with list of simple entities', () => {
- const input = [{ id: 'id', name: 'foo' }, { id: 'id', name: 'bar' }];
- const result = idCodec.encode(input);
- expect(result).toEqual([{ id: ENCODED, name: 'foo' }, { id: ENCODED, name: 'bar' }]);
- });
- it('does not throw with an empty list', () => {
- const input = [];
- const result = idCodec.encode(input);
- expect(() => idCodec.encode(input)).not.toThrow();
- });
- it('works with nested list of simple entities', () => {
- const input = {
- items: [{ id: 'id', name: 'foo' }, { id: 'id', name: 'bar' }],
- };
- const result = idCodec.encode(input);
- expect(result).toEqual({
- items: [{ id: ENCODED, name: 'foo' }, { id: ENCODED, name: 'bar' }],
- });
- });
- it('works with large and nested list', () => {
- const length = 100;
- const input = {
- items: Array.from({ length }).map(() => ({
- id: 'id',
- name: { bar: 'baz' },
- foo: 'yo',
- friends: [{ id: 'id', name: { first: 'boris', id: 'id' } }],
- })),
- };
- const result = idCodec.encode(input);
- expect(result.items.length).toBe(length);
- expect(result.items[0].id).toBe(ENCODED);
- expect(result.items[0].friends[0].id).toBe(ENCODED);
- expect(result.items[0].friends[0].name.id).toBe(ENCODED);
- });
- it('works with nested list of nested lists', () => {
- const input = {
- items: [
- {
- id: 'id',
- friends: [{ id: 'id' }, { id: 'id' }],
- },
- {
- id: 'id',
- friends: [{ id: 'id' }, { id: 'id' }],
- },
- ],
- };
- const result = idCodec.encode(input);
- expect(result).toEqual({
- items: [
- {
- id: ENCODED,
- friends: [{ id: ENCODED }, { id: ENCODED }],
- },
- {
- id: ENCODED,
- friends: [{ id: ENCODED }, { id: ENCODED }],
- },
- ],
- });
- });
- it('transformKeys can be customized', () => {
- const input = { id: 'id', name: 'foo' };
- const result = idCodec.encode(input, ['name']);
- expect(result).toEqual({ id: ENCODED, name: ENCODED });
- });
- });
- describe('decode()', () => {
- it('works with a string', () => {
- const input = 'id';
- const result = idCodec.decode(input);
- expect(result).toEqual(DECODED);
- });
- it('works with a number', () => {
- const input = 123;
- const result = idCodec.decode(input);
- expect(result).toEqual(DECODED);
- });
- it('works with simple entity', () => {
- const input = { id: 'id', name: 'foo' };
- const result = idCodec.decode(input);
- expect(result).toEqual({ id: DECODED, name: 'foo' });
- });
- it('works with 2-level nested entities', () => {
- const input = {
- id: 'id',
- friend: { id: 'id' },
- };
- const result = idCodec.decode(input);
- expect(result).toEqual({
- id: DECODED,
- friend: { id: DECODED },
- });
- });
- it('works with 3-level nested entities', () => {
- const input = {
- id: 'id',
- friend: {
- dog: { id: 'id' },
- },
- };
- const result = idCodec.decode(input);
- expect(result).toEqual({
- id: DECODED,
- friend: {
- dog: { id: DECODED },
- },
- });
- });
- it('works with list of simple entities', () => {
- const input = [{ id: 'id', name: 'foo' }, { id: 'id', name: 'bar' }];
- const result = idCodec.decode(input);
- expect(result).toEqual([{ id: DECODED, name: 'foo' }, { id: DECODED, name: 'bar' }]);
- });
- it('works with nested list of simple entities', () => {
- const input = {
- items: [{ id: 'id', name: 'foo' }, { id: 'id', name: 'bar' }],
- };
- const result = idCodec.decode(input);
- expect(result).toEqual({
- items: [{ id: DECODED, name: 'foo' }, { id: DECODED, name: 'bar' }],
- });
- });
- it('works with large and nested list', () => {
- const length = 100;
- const input = {
- items: Array.from({ length }).map(() => ({
- id: 'id',
- name: { bar: 'baz' },
- foo: 'yo',
- friends: [{ id: 'id', name: { first: 'boris', id: 'id' } }],
- })),
- };
- const result = idCodec.decode(input);
- expect(result.items.length).toBe(length);
- expect(result.items[0].id).toBe(DECODED);
- expect(result.items[0].friends[0].id).toBe(DECODED);
- expect(result.items[0].friends[0].name.id).toBe(DECODED);
- });
- it('works with nested list of nested lists', () => {
- const input = {
- items: [
- {
- id: 'id',
- friends: [{ id: 'id' }, { id: 'id' }],
- },
- {
- id: 'id',
- friends: [{ id: 'id' }, { id: 'id' }],
- },
- ],
- };
- const result = idCodec.decode(input);
- expect(result).toEqual({
- items: [
- {
- id: DECODED,
- friends: [{ id: DECODED }, { id: DECODED }],
- },
- {
- id: DECODED,
- friends: [{ id: DECODED }, { id: DECODED }],
- },
- ],
- });
- });
- it('transformKeys can be customized', () => {
- const input = { name: 'foo' };
- const result = idCodec.decode(input, ['name']);
- expect(result).toEqual({ name: DECODED });
- });
- it('id keys is still implicitly decoded when transformKeys are defined', () => {
- const input = { id: 'id', name: 'foo' };
- const result = idCodec.decode(input, ['name']);
- expect(result).toEqual({ id: DECODED, name: DECODED });
- });
- it('transformKeys works for nested matching keys', () => {
- const input = {
- input: {
- id: 'id',
- featuredAssetId: 'id',
- foo: 'bar',
- },
- };
- const result = idCodec.decode(input, ['featuredAssetId']);
- expect(result).toEqual({
- input: {
- id: DECODED,
- featuredAssetId: DECODED,
- foo: 'bar',
- },
- });
- });
- it('transformKeys works for nested matching key array', () => {
- const input = {
- input: {
- id: 'id',
- assetIds: ['id1', 'id2', 'id3'],
- foo: 'bar',
- },
- };
- const result = idCodec.decode(input, ['assetIds']);
- expect(result).toEqual({
- input: {
- id: DECODED,
- assetIds: [DECODED, DECODED, DECODED],
- foo: 'bar',
- },
- });
- });
- it('transformKeys works for multiple nested keys', () => {
- const input = {
- input: {
- id: 'id',
- featuredAssetId: 'id',
- assetIds: ['id1', 'id2', 'id3'],
- foo: 'bar',
- },
- };
- const result = idCodec.decode(input, ['featuredAssetId', 'assetIds']);
- expect(result).toEqual({
- input: {
- id: DECODED,
- featuredAssetId: DECODED,
- assetIds: [DECODED, DECODED, DECODED],
- foo: 'bar',
- },
- });
- });
- });
- });
|