Exemplo n.º 1
0
 const _loadCurrentCollection = ({
   addonsResponse = createFakeCollectionAddonsListResponse(),
   detail = createFakeCollectionDetail(),
 } = {}) => {
   return loadCurrentCollection({
     addonsResponse,
     detail,
   });
 };
Exemplo n.º 2
0
  it("does not update the page when removeAddon is called and the current page isn't the last page", () => {
    const authorUserId = 11;
    const { store } = dispatchSignInActions({ userId: authorUserId });

    const addons = createFakeCollectionAddons();
    const addonId = addons[0].addon.id;
    const detail = createFakeCollectionDetail({
      authorId: authorUserId,
      // This will simulate 1 item on the 3nd page.
      count: DEFAULT_API_PAGE_SIZE * 2 + 1,
    });
    const errorHandler = createStubErrorHandler();
    const fakeDispatch = sinon.spy(store, 'dispatch');
    const page = '1';
    const sort = COLLECTION_SORT_NAME;
    const location = createFakeLocation({
      query: { page, collection_sort: sort },
    });
    const history = createFakeHistory({ location });

    store.dispatch(
      loadCurrentCollection({
        addons,
        detail,
        pageSize: DEFAULT_API_PAGE_SIZE,
      }),
    );

    const root = renderComponent({
      editing: true,
      errorHandler,
      history,
      location,
      store,
    });

    fakeDispatch.resetHistory();

    // This simulates the user clicking the "Remove" button on the
    // EditableCollectionAddon component.
    root.instance().removeAddon(addonId);
    sinon.assert.calledWith(
      fakeDispatch,
      removeAddonFromCollection({
        addonId,
        errorHandlerId: errorHandler.id,
        filters: { page, collectionSort: sort },
        slug: detail.slug,
        username: detail.author.username,
      }),
    );
    sinon.assert.callCount(fakeDispatch, 1);

    sinon.assert.notCalled(history.push);
  });
Exemplo n.º 3
0
 const _loadCurrentCollection = ({
   store,
   addons = createFakeCollectionAddons(),
   detail = defaultCollectionDetail,
   pageSize = DEFAULT_API_PAGE_SIZE,
 }) => {
   store.dispatch(
     loadCurrentCollection({
       addons,
       detail,
       pageSize,
     }),
   );
 };
Exemplo n.º 4
0
  it('dispatches updateCollectionAddon when saveNote is called', () => {
    const authorUserId = 11;
    const { store } = dispatchSignInActions({ userId: authorUserId });

    const addons = createFakeCollectionAddons();
    const addonId = addons[0].addon.id;
    const detail = createFakeCollectionDetail({
      authorId: authorUserId,
    });
    const errorHandler = createStubErrorHandler();
    const fakeDispatch = sinon.spy(store, 'dispatch');
    const page = 123;
    const notes = 'These are some notes.';
    const sort = COLLECTION_SORT_NAME;

    store.dispatch(
      loadCurrentCollection({
        addons,
        detail,
        pageSize: DEFAULT_API_PAGE_SIZE,
      }),
    );
    const root = renderComponent({
      editing: true,
      errorHandler,
      location: createFakeLocation({ query: { page, collection_sort: sort } }),
      store,
    });

    fakeDispatch.resetHistory();

    // This simulates the user clicking the "Save" button on the
    // EditableCollectionAddon component's comment form.
    root.instance().saveNote(addonId, errorHandler, notes);
    sinon.assert.callCount(fakeDispatch, 1);
    sinon.assert.calledWith(
      fakeDispatch,
      updateCollectionAddon({
        addonId,
        errorHandlerId: errorHandler.id,
        notes,
        filters: { page, collectionSort: sort },
        slug: detail.slug,
        username: detail.author.username,
      }),
    );
  });
      it('sends a request to the collections API', async () => {
        const state = sagaTester.getState();
        const params = getParams({ lang: state.api.lang });

        const collectionDetailResponse = createFakeCollectionDetail(params);

        mockApi
          .expects('createCollection')
          .withArgs({
            api: state.api,
            defaultLocale: undefined,
            description: params.description,
            name: params.name,
            slug,
            username,
          })
          .once()
          .returns(Promise.resolve(collectionDetailResponse));

        _createCollection(params);

        const expectedLoadAction = loadCurrentCollection({
          addons: [],
          detail: localizeCollectionDetail({
            detail: collectionDetailResponse,
            lang: state.api.lang,
          }),
          pageSize: null,
        });

        const loadAction = await sagaTester.waitFor(expectedLoadAction.type);
        expect(loadAction).toEqual(expectedLoadAction);

        const { lang, clientApp } = clientData.state.api;
        const expectedAction = pushLocation(
          `/${lang}/${clientApp}/collections/${username}/${slug}/edit/`,
        );

        await sagaTester.waitFor(expectedAction.type);
        mockApi.verify();
      });
Exemplo n.º 6
0
  it('dispatches deleteCollection when onDelete is called', () => {
    const authorUserId = 11;
    const slug = 'some-slug';
    const username = '******';
    const { store } = dispatchSignInActions({ userId: authorUserId });

    store.dispatch(
      loadCurrentCollection({
        addons: createFakeCollectionAddons(),
        detail: createFakeCollectionDetail({
          authorId: authorUserId,
          authorUsername: username,
          slug,
        }),
        pageSize: DEFAULT_API_PAGE_SIZE,
      }),
    );

    const dispatchSpy = sinon.spy(store, 'dispatch');
    const preventDefaultSpy = sinon.spy();
    const errorHandler = createStubErrorHandler();

    const wrapper = renderComponent({ errorHandler, store });

    dispatchSpy.resetHistory();

    // This emulates a user clicking the delete button and confirming.
    const onDelete = wrapper.find(ConfirmButton).prop('onConfirm');
    onDelete(createFakeEvent({ preventDefault: preventDefaultSpy }));

    sinon.assert.calledOnce(preventDefaultSpy);
    sinon.assert.callCount(dispatchSpy, 1);
    sinon.assert.calledWith(
      dispatchSpy,
      deleteCollection({
        errorHandlerId: errorHandler.id,
        slug,
        username,
      }),
    );
  });
    it('calls the API to fetch a collection', async () => {
      const state = sagaTester.getState();

      const collectionAddons = createFakeCollectionAddonsListResponse();
      const collectionDetail = createFakeCollectionDetail();

      mockApi
        .expects('getCollectionDetail')
        .withArgs({
          api: state.api,
          slug,
          username,
        })
        .once()
        .returns(Promise.resolve(collectionDetail));

      mockApi
        .expects('getCollectionAddons')
        .withArgs({
          api: state.api,
          filters,
          slug,
          username,
        })
        .once()
        .returns(Promise.resolve(collectionAddons));

      _fetchCurrentCollection({ filters, slug, username });

      const expectedLoadAction = loadCurrentCollection({
        addons: collectionAddons.results,
        detail: collectionDetail,
        pageSize: DEFAULT_API_PAGE_SIZE,
      });

      const loadAction = await sagaTester.waitFor(expectedLoadAction.type);
      expect(loadAction).toEqual(expectedLoadAction);
      mockApi.verify();
    });