redux-saga/effects#apply JavaScript Examples

The following examples show how to use redux-saga/effects#apply. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: reminder.js    From jc-calendar with MIT License 6 votes vote down vote up
export function* newReminder(action) {
  const initialDate = action.payload;
  let initialDateTime = yield apply(DateTime, 'local');

  if (initialDate) {
    const parsed = DateTime.fromFormat(initialDate, DATE_FORMAT);
    if (parsed.isValid) {
      initialDateTime = initialDateTime.set({
        year: parsed.year,
        month: parsed.month,
        day: parsed.day,
      });
    }
  }

  const initialColor = DEFAULT_COLOR;

  const reminder = {
    id: null,
    description: '',
    color: initialColor,
    dateTime: initialDateTime.toMillis(),
    city: '',
  };

  yield call(openReminder, reminder);
}
Example #2
Source File: reminder.test.js    From jc-calendar with MIT License 4 votes vote down vote up
describe('sagas::ui::reminder', () => {
  test('openReminder', () => {
    const iterator = openReminder({
      city: 'Lorem Ipsum',
      color: 'indigo-600',
      dateTime: 1608247620000,
      description: 'TEST EDITED',
      id: 'hqsd5mj1g7o13jkyhdeg',
    });

    expect(iterator.next().value).toEqual(
      put(
        reminderUIActions.openReminder({
          city: 'Lorem Ipsum',
          color: 'indigo-600',
          dateTime: 1608247620000,
          description: 'TEST EDITED',
          id: 'hqsd5mj1g7o13jkyhdeg',
        })
      )
    );
  });

  test('newReminder', () => {
    let iterator = newReminder(reminderUIActions.newReminder());
    expect(iterator.next().value).toEqual(apply(DateTime, 'local'));

    const now = DateTime.fromMillis(1608247620000);
    expect(iterator.next(now).value).toEqual(
      call(openReminder, {
        id: null,
        description: '',
        color: DEFAULT_COLOR,
        dateTime: 1608247620000,
        city: '',
      })
    );

    iterator = newReminder(
      // '2020-12-17' is the date part from 1608247620000
      reminderUIActions.newReminder('2020-12-17')
    );
    expect(iterator.next().value).toEqual(apply(DateTime, 'local'));
    expect(iterator.next(now).value).toEqual(
      call(openReminder, {
        id: null,
        description: '',
        color: DEFAULT_COLOR,
        dateTime: 1608247620000,
        city: '',
      })
    );
  });

  test('editReminder', () => {
    const iterator = editReminder(
      reminderUIActions.editReminder({
        city: 'Lorem Ipsum',
        color: 'indigo-600',
        dateTime: 1608247620000,
        description: 'TEST EDITED',
        id: 'hqsd5mj1g7o13jkyhdeg',
      })
    );

    expect(iterator.next().value).toEqual(
      call(openReminder, {
        city: 'Lorem Ipsum',
        color: 'indigo-600',
        dateTime: 1608247620000,
        description: 'TEST EDITED',
        id: 'hqsd5mj1g7o13jkyhdeg',
      })
    );
  });

  test('submitReminder - new', () => {
    const iterator = submitReminder(
      reminderUIActions.submitReminder({
        city: 'Lorem Ipsum',
        color: 'indigo-600',
        date: '2020-12-11',
        time: '10:55',
        description: 'TEST',
        id: null,
      })
    );

    expect(iterator.next().value).toEqual(call(generateUUID));
    expect(iterator.next('some-id').value).toEqual(
      put(
        setReminder({
          id: 'some-id',
          description: 'TEST',
          color: 'indigo-600',
          dateTime: dateTimeStringsToMillis('2020-12-11', '10:55'),
          city: 'Lorem Ipsum',
        })
      )
    );
    expect(iterator.next().value).toEqual(
      put(
        setDateReminder({
          date: '2020-12-11',
          reminderId: 'some-id',
        })
      )
    );
    expect(iterator.next().value).toEqual(
      put(reminderUIActions.closeReminder())
    );
  });

  test('submitReminder - existing', () => {
    const iterator = submitReminder(
      reminderUIActions.submitReminder({
        city: 'Lorem Ipsum',
        color: 'indigo-600',
        date: '2020-12-11',
        time: '10:55',
        description: 'TEST',
        id: 'some-existing-id',
      })
    );

    expect(iterator.next().value).toEqual(
      put(
        setReminder({
          id: 'some-existing-id',
          description: 'TEST',
          color: 'indigo-600',
          dateTime: dateTimeStringsToMillis('2020-12-11', '10:55'),
          city: 'Lorem Ipsum',
        })
      )
    );
    expect(iterator.next().value).toEqual(
      put(
        setDateReminder({
          date: '2020-12-11',
          reminderId: 'some-existing-id',
        })
      )
    );
    expect(iterator.next().value).toEqual(
      put(reminderUIActions.closeReminder())
    );
  });
});