Home Reference Source

application/__tests__/current-user-test.js

jest.unmock('./../current-user')

import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils';
import CurrentUser from './../current-user';

describe('CurrentUser', () => {
    let userData;

    beforeEach(() => {
        const pad = 'istenchars';
        const user = {
            'id': 1,
            'uuid': '123-456',
            'name': 'Test Testerson',
            'email': 'test@example.com',
            'public_key': 'pk_123',
            'private_key': 'sk_123',
            'gravatar': 'example.com'
        };

        userData = pad + btoa(JSON.stringify(user));
        window._app_udata = userData;
    });

    it('returns data', () => {
        CurrentUser.init(userData);

        expect(CurrentUser.getId()).toEqual(1);
        expect(CurrentUser.getUuid()).toEqual('123-456');
        expect(CurrentUser.getName()).toEqual('Test Testerson');
        expect(CurrentUser.getEmail()).toEqual('test@example.com');
        expect(CurrentUser.getPublicToken()).toEqual('pk_123');
        expect(CurrentUser.getPrivateToken()).toEqual('sk_123');
        expect(CurrentUser.getGravatar()).toEqual('example.com');
    });
});

describe('CurrentUser with bad pad', () => {
    let userData;

    beforeEach(() => {
        const pad = '';
        const user = {
            'id': 1,
            'uuid': '123-456',
            'name': 'Test Testerson',
            'email': 'test@example.com',
            'public_key': 'pk_123',
            'private_key': 'sk_123',
            'gravatar': 'example.com'
        };

        userData = pad + btoa(JSON.stringify(user));
        window._app_udata = userData;
    });

    it('throws error', () => {
        const cb = () => {
            CurrentUser.init(userData);
        }
        expect(cb).toThrow();
    });
});