it('should start a new user request and cancel an existing one', () => {
        const dummyTask = createMockTask();
        const dummyTask2 = createMockTask();
        const dummyAction = startUserRequest(3);
        const dummyAction2 = startUserRequest(4);
        const gen = saga();
        let next = gen.next();

        expect(next.value).to.eql(take([START_USER_REQUEST, CANCEL_USER_REQUEST]));
        next = gen.next(dummyAction);

        expect(next.value).to.eql(fork(getUser, 3));
        next = gen.next(dummyTask);

        expect(next.value).to.eql(put(userRequestStarted()));
        next = gen.next();

        expect(next.value).to.eql(take([START_USER_REQUEST, CANCEL_USER_REQUEST]));
        next = gen.next(dummyAction2);

        expect(next.value).to.eql(cancel(dummyTask));
        next = gen.next();

        expect(next.value).to.eql(fork(getUser, 4));
        next = gen.next(dummyTask2);

        expect(next.value).to.eql(put(userRequestStarted()));
        next = gen.next();

        expect(next.value).to.eql(take([START_USER_REQUEST, CANCEL_USER_REQUEST]));
      });
  describe('logged in saga', () => {
    const generator = loggedInSaga();
    const dataSagaInstance = createMockTask();
    const refreshTokenSagaInstance = createMockTask();

    it('should start a saga for getting the user\'s data', () => {
      generator.next().value.should.deep.equal(fork(dataSaga));
    });

    it('should start a saga to keep the user\'s token refreshed', () => {
      generator.next(dataSagaInstance).value.should.deep.equal(fork(refreshTokenSaga));
    });

    it('should watch for either a log out or bad token refresh action', () => {
      const raceCondition = {
        manualLogOut: take('LOG_OUT'),
        unableToRefresh: take('REFRESH_TOKEN_FAILURE'),
      };
      generator.next(refreshTokenSagaInstance).value.should.deep.equal(race(raceCondition));
    });

    it('should cancel the data and the refresh token sagas', () => {
      generator.next().value.should.deep.equal(cancel(dataSagaInstance));
      generator.next().value.should.deep.equal(cancel(refreshTokenSagaInstance));
    });

    it('should dispatch a clear auth action', () => {
      generator.next().value.should.deep.equal(put(creators.clearAuth()));
    });

    it('should be done', () => {
      generator.next().done.should.equal(true);
    });
  });
Esempio n. 3
0
    it('should watch every PROGRESSBAR_START action', () => {
      const gen = watch();

      assert.deepEqual(
        gen.next().value,
        take(types.PROGRESSBAR_START)
      );

      assert.deepEqual(
        gen.next(true).value,
        fork(startProgressBarTask)
      );

      const task = createMockTask(startProgressBarTask);

      assert.deepEqual(
        gen.next(task).value,
        take(types.PROGRESSBAR_STOP)
      );

      assert.deepEqual(
        gen.next().value,
        cancel(task)
      );
    });
Esempio n. 4
0
        it('cancels the previous action when a new matching action is dispatched then proceeds with the new one', () => {
            const task = createMockTask();
            const tasks = { posts: task };
            const accumulations = { posts: [1, 2] };
            const finalize = finalizeFactory(tasks, accumulations);

            const saga = accumulateFactory(tasks, accumulations, finalize)({
                type: 'ACCUMULATE_ACTION',
                payload: { ids: [3, 4] },
                meta: {
                    accumulate: crudGetMany,
                    accumulateValues: (accumulations, action) => [
                        ...(accumulations || []),
                        ...action.payload.ids,
                    ],
                    accumulateKey: 'posts',
                },
            });

            expect(saga.next().value).toEqual(cancel(task));

            expect(saga.next().value).toEqual(
                fork(finalize, 'posts', crudGetMany)
            );

            expect(accumulations).toEqual({
                posts: [1, 2, 3, 4],
            });
        });
    describe('loginFlow', () => {
        const generator = loginFlow({
            type: LOGIN_REQUEST,
            username: '******',
            password: '******'
        })
        const task = createMockTask()

        it('should fork a authorize test', () => {
            const expected = fork(authorize, {
                username: '******',
                password: '******'
            })
            const actual = generator.next().value
            assert.deepEqual(expected, actual)
        })

        it('should take cancel login action', () => {
            const expected = take(LOGIN_CANCEL)
            const actual = generator.next(task).value
            assert.deepEqual(expected, actual)
        })

        it('should cancel the login task', () => {
            const expected = cancel(task)
            const actual = generator.next().value
            assert.deepEqual(expected, actual)
        })
    })
      it('should start an user request for the first time and be canceled', () => {
        const dummyAction = startUserRequest(3);
        const dummyNewAction = cancelUserRequest();
        const dummyTask = createMockTask();

        const gen = saga();
        let next = gen.next();

        expect(next.value).to.eql(take(START_USER_REQUEST));
        next = gen.next(dummyAction);

        expect(next.value).to.eql(fork(getUser, dummyAction.id));
        next = gen.next(dummyTask);

        expect(next.value).to.eql(put(userRequestStarted()));
        next = gen.next();

        expect(next.value).to.eql(
          take([CANCEL_USER_REQUEST, USER_REQUEST_FAILED, USER_REQUEST_SUCCEED])
        );
        next = gen.next(dummyNewAction);

        expect(next.value).to.eql(cancel(dummyTask));
        next = gen.next();

        expect(next.value).to.eql(take(START_USER_REQUEST));
      });
test('searchFlow if take CANCEL_SEARCH should cancel search else fetch photos', t => {
  const generator = searchFlow();
  const task = createMockTask();

  t.deepEqual(generator.next().value, fork(searchResult));
  t.deepEqual(generator.next(task).value, take(CANCEL_SEARCH));
  t.deepEqual(generator.next().value, cancel(task));
});
test('increment asyncIncrementFlow test', t => {
  const generator = asyncIncrementFlow();
  const task = createMockTask();

  t.deepEqual(generator.next().value, fork(incrementCountAsync));
  t.deepEqual(generator.next(task).value, take(CANCEL_INCREMENT_COUNTER_ASYNC));
  t.deepEqual(generator.next().value, cancel(task));
});
Esempio n. 9
0
test('saga', (t) => {
  const expect = fromGenerator(t, testSaga())

  expect.next().take('ACTION')
  expect.nextIs(put({type: 'NEXTIS'}))
  expect.next().put({type: 'PUT'})
  expect.next().call(delay, 0)
  expect.next().cps(request, 'CPS')
  expect.next().fork(bgSync)
  const mockTask1 = createMockTask()
  expect.next(mockTask1).spawn(bgSync)
  const mockTask2 = createMockTask()
  expect.next(mockTask2).join(mockTask1)
  expect.next().cancel(mockTask2)
  expect.next().select(getFromState)
  expect.next(getFromState()).put('SELECTED')
  expect.throwNext().put({type: 'ERROR'})
})
Esempio n. 10
0
test('the effect and saga constants line up', t => {
  t.is(getEffectName(Effects.take()), SagaConstants.TAKE)
  t.is(getEffectName(Effects.put({})), SagaConstants.PUT)
  t.is(getEffectName(Effects.call(() => {})), SagaConstants.CALL)
  t.is(getEffectName(Effects.cps(() => {})), SagaConstants.CPS)
  t.is(getEffectName(Effects.fork(lol)), SagaConstants.FORK)
  t.is(getEffectName(Effects.join(createMockTask())), SagaConstants.JOIN)
  t.is(getEffectName(Effects.race([createMockTask(), createMockTask()])), SagaConstants.RACE)
  t.is(getEffectName(Effects.cancel(createMockTask())), SagaConstants.CANCEL)
  t.is(getEffectName(Effects.select(() => {})), SagaConstants.SELECT)
  t.is(getEffectName([]), SagaConstants.PARALLEL)
  t.is(getEffectName(lol()), SagaConstants.ITERATOR)
  t.is(getEffectName(null), SagaConstants.UNKNOWN)
  t.is(getEffectName(''), SagaConstants.UNKNOWN)
  t.is(getEffectName(1), SagaConstants.UNKNOWN)
  t.is(getEffectName({}), SagaConstants.UNKNOWN)
  t.is(getEffectName(undefined), SagaConstants.UNKNOWN)
})
Esempio n. 11
0
  it('fork updateAddress and cancel it when the node address was changed', () => {
    const gen = cancelUpdateAddressTask();

    expect(gen.next().value)
      .toFork(updateAddress);

    const updateAddressTask = createMockTask();

    expect(gen.next(updateAddressTask).value)
      .toTake(CHANGE_NODE_ADDRESS);

    expect(gen.next().value)
      .toCancel(updateAddressTask);

    expect(gen.next().done)
      .toBe(true);
  });
Esempio n. 12
0
          first time and listen for start/cancel request`, () => {
        const dummyAction = startUserRequest(3);
        const dummyTask = createMockTask();

        const gen = saga();
        let next = gen.next();

        expect(next.value).to.eql(take([START_USER_REQUEST, CANCEL_USER_REQUEST]));
        next = gen.next(dummyAction);

        expect(next.value).to.eql(fork(getUser, 3));
        next = gen.next(dummyTask);

        expect(next.value).to.eql(put(userRequestStarted()));
        next = gen.next();

        expect(next.value).to.eql(take([START_USER_REQUEST, CANCEL_USER_REQUEST]));
      });
Esempio n. 13
0
        it('waits for a 50ms delay before dispatching the action', () => {
            const task = createMockTask();
            const tasks = { posts: task };
            const accumulations = { posts: [1, 2] };
            const saga = finalizeFactory(tasks, accumulations)(
                'posts',
                crudGetMany
            );

            expect(saga.next().value).toEqual(call(delay, 50));

            expect(saga.next().value).toEqual(
                put(crudGetMany('posts', [1, 2]))
            );

            saga.next(); // Ends the saga
            expect(tasks).toEqual({});
            expect(accumulations).toEqual({});
        });
Esempio n. 14
0
      it('should do nothing when a new user request is made but one is in progress', () => {
        const dummyAction = startUserRequest(3);
        const dummyTask = createMockTask();

        const gen = saga();
        let next = gen.next();

        expect(next.value).to.eql(take(START_USER_REQUEST));
        next = gen.next(dummyAction);

        expect(next.value).to.eql(fork(getUser, dummyAction.id));
        next = gen.next(dummyTask);

        expect(next.value).to.eql(put(userRequestStarted()));
        next = gen.next();

        // Generator hanging here because it's waiting for any of the below 3 actions but
        // the user is sending more startUserRequest actions.
        expect(next.value).to.eql(
          take([CANCEL_USER_REQUEST, USER_REQUEST_FAILED, USER_REQUEST_SUCCEED])
        );
      });
Esempio n. 15
0
        it('cancels the previous action when a new matching action is dispatched then proceeds with the new one', () => {
            const task = createMockTask();
            const tasks = { posts: task };
            const accumulations = {
                posts: [1, 2],
            };
            const finalize = finalizeFactory(tasks, accumulations);

            const saga = accumulateFactory(tasks, accumulations, finalize)({
                payload: { resource: 'posts', ids: [2, 3] },
                meta: { accumulate: crudGetMany },
            });

            expect(saga.next().value).toEqual(cancel(task));

            expect(saga.next().value).toEqual(
                fork(finalize, 'posts', crudGetMany)
            );

            expect(accumulations).toEqual({
                posts: [1, 2, 3],
            });
        });
Esempio n. 16
0
 beforeEach(() => {
   mockTask = createMockTask();
   otherMockTask = createMockTask();
 })