Skip to content

Instantly share code, notes, and snippets.

@0on
Last active March 19, 2018 10:53
Show Gist options
  • Select an option

  • Save 0on/c5ae0be653fd2d9d29014d2929e89b0d to your computer and use it in GitHub Desktop.

Select an option

Save 0on/c5ae0be653fd2d9d29014d2929e89b0d to your computer and use it in GitHub Desktop.

Revisions

  1. 0on renamed this gist Mar 19, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. 0on created this gist Mar 19, 2018.
    196 changes: 196 additions & 0 deletions test
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,196 @@
    import favIcon from 'favicon-notification';

    import { ORGANIZATION_TYPES } from '../../common/constants';
    import notify from '../../common/notify';
    import * as actions from '../updaterActions';
    import { initialState as initial } from '../../reducers/updater';
    import * as ttt from '../communityActions'
    import {
    getApiMock,
    generateMock,
    getMockStore,
    fetchPromise,
    } from '../../common/testUtils';

    const mockStore = getMockStore();
    const api = getApiMock();
    const favIconMock = generateMock(favIcon);
    const notifyMock = generateMock(notify);
    const clientId = 1;
    const teamId = 2;
    const teams = [{ teamId, type: ORGANIZATION_TYPES.COMMON }];
    const initialState = {
    data: {
    mainInfo: {
    clientId,
    teamId,
    teams,
    },
    },
    updater: {
    ...initial,
    },
    access: {
    '1': {
    teamId: 1,
    organization: 'COMMON',
    },
    '2': {
    teamId: 2,
    organization: 'COMMON',
    },
    '3': {
    teamId: 3,
    organization: 'COMMON',
    }
    }
    };

    const clearMocks = () => {
    notifyMock.requestPermission.mockClear();
    notifyMock.createNotification.mockClear();
    favIconMock.add.mockClear();
    api.fetch.mockClear();
    favIconMock.remove.mockClear();
    };

    beforeEach(() => {
    clearMocks();
    });

    describe('actions/updaterActions/setForUpdate', () => {
    it('correct action', () => {
    const res = 'res';
    const expexted = { type: 'UPDATER_SET', shouldUpdate: res };
    expect(actions.setForUpdate(res)).toEqual(expexted);
    });
    });

    describe('actions/updaterActions/clearUpdate', () => {
    it('correct action', () => {
    const expexted = { type: 'UPDATER_CLEAR' };
    expect(actions.clearUpdate()).toEqual(expexted);
    });
    });

    describe('actions/updaterActions/getCounters', () => {
    it('full action', async () => {
    const store = mockStore(initialState);
    const messageCount = 1;
    const expectedCounter = {
    unreadMessageCount: messageCount,
    unreadInboxCount: 0,
    unreadMessageCountMap: {
    2: messageCount,
    },
    };
    api.fetch.mockReturnValue(fetchPromise({
    [teamId]: {
    ...expectedCounter,
    },
    }));
    notifyMock.permission = notifyMock.PERMISSION_DEFAULT;

    await store.dispatch(actions.getCounters());
    const runActions = store.getActions();

    expect(api.fetch).toBeCalledWith(
    'aaa', 'GET', {a: 1}
    )
    expect(api.fetch).toBeCalledWith(
    'bbb', 'GET', {b:'b'}
    )
    expect(favIconMock.add).toHaveBeenCalled();
    expect(notifyMock.requestPermission).toHaveBeenCalled();
    expect(api.fetch).toHaveBeenCalled();
    expect(api.fetch).toBeCalledWith(
    '/api/stream/getUnreadCountersByClient',
    'GET',
    { clientId, teamId }
    );
    expect(runActions.length).toEqual(1);
    expect(runActions[0].type).toEqual('UPDATER_SET_COUNTERS');
    expect(runActions[0].counters).toEqual(expectedCounter);
    expect(runActions[0].otherCounters).toEqual({});
    });

    it('no permissions request', async () => {
    const store = mockStore(initialState);
    notifyMock.permission = notifyMock.PERMISSION_GRANTED;

    await store.dispatch(actions.getCounters());
    expect(notifyMock.requestPermission).not.toHaveBeenCalled();
    });

    it('notification created', async () => {
    const store = mockStore(initialState);
    const messageCount = 2;
    const expectedCounter = {
    unreadMessageCount: messageCount,
    unreadInboxCount: 0,
    unreadMessageCountMap: {
    3: messageCount,
    },
    };
    api.fetch.mockReturnValueOnce(fetchPromise({
    [teamId]: {
    ...expectedCounter,
    },
    }));
    notifyMock.permission = notifyMock.PERMISSION_GRANTED;

    await store.dispatch(actions.getCounters());
    expect(notifyMock.createNotification).toHaveBeenCalled();
    });

    it('favIcon remove', async () => {
    const store = mockStore(initialState);
    const messageCount = 0;
    const expectedCounter = {
    unreadMessageCount: messageCount,
    unreadInboxCount: 0,
    unreadMessageCountMap: {
    3: messageCount,
    },
    };
    api.fetch.mockReturnValueOnce(fetchPromise({
    [teamId]: {
    ...expectedCounter,
    },
    }));

    await store.dispatch(actions.getCounters());
    expect(favIcon.remove).toHaveBeenCalled();
    });
    });

    describe('aaa', () => {
    it('aaa', async () => {

    const store = mockStore({
    ...initialState,
    communities: {
    activeClientId: 123,
    activePractice: 23,
    communityOrganizations: [{id : 23, clients: [{id: 123, status: 'ACTIVE'}]}]
    }
    });
    await store.dispatch(ttt.blockClient());
    // expect(api).toHaveBeenCalledTimes(2)
    expect(api.fetch).lastCalledWith(
    '/api/community/getCommunityOrganizations',
    'GET',
    { teamId, clientId }
    )
    expect(api.fetch).toHaveBeenCalledWith(
    '/api/community/blockUser',
    'POST',
    {
    teamId,
    communityId: 23,
    userId: 123,
    currentStatus: 'ACTIVE',
    }
    )
    })
    })