All files / src/__tests__ payload.ts

100% Statements 26/26
100% Branches 0/0
100% Functions 5/5
100% Lines 26/26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56    1x 1x           1x         1x         1x         1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x     1x 1x 1x     1x 1x 1x     1x 1x   1x 1x      
import AuthPayload, { isEmpty } from '../payload';
 
describe('Auth Payload', () => {
  const payload = new AuthPayload({
    uId: 'id',
    cId: 'companyId',
    scope: 'scope'
  });
 
  const user = {
    id: 'user_123',
    companyId: 'company_123',
    scope: 'a:r:w'
  };
  const reverseUser = {
    uId: 'user_123',
    cId: 'company_123',
    scope: 'a:r:w'
  };
  const randomKeys = {
    name: 'Luis',
    lastName: 'Alvarez'
  };
 
  it('Has a proper check for empty values', () => {
    expect(isEmpty(null)).toBe(true);
    expect(isEmpty(undefined)).toBe(true);
    expect(isEmpty({})).toBe(true);
    expect(isEmpty([])).toBe(true);
    expect(isEmpty(' ')).toBe(true);
 
    expect(isEmpty('something')).toBe(false);
    expect(isEmpty(0)).toBe(false);
    expect(isEmpty(false)).toBe(false);
    expect(isEmpty([0])).toBe(false);
    expect(isEmpty({ '0': 0 })).toBe(false);
  });
 
  it('Creates a payload', () => {
    expect(payload.create({})).toEqual({});
    expect(payload.create({ ...randomKeys, ...user })).toEqual(reverseUser);
  });
 
  it('Parses a payload', () => {
    expect(payload.parse({})).toEqual({});
    expect(payload.parse({ ...randomKeys, ...reverseUser })).toEqual(user);
  });
 
  it('Always returns the same object with empty call to constructor', () => {
    const empty = new AuthPayload();
 
    expect(empty.create(user)).toEqual(user);
    expect(empty.parse(reverseUser)).toEqual(reverseUser);
  });
});