Example #1
0
  handleSuggestionsFetchRequested = ({
    value,
  }: OnSuggestionsFetchRequestedParams) => {
    if (!value) {
      log.debug(oneLine`Ignoring suggestions fetch requested because
        value is not supplied: ${value}`);
      return;
    }

    if (value.length < SEARCH_TERM_MIN_LENGTH) {
      log.debug(oneLine`Ignoring suggestions fetch because query
      does not meet the required length (${SEARCH_TERM_MIN_LENGTH})`);

      this.props.dispatch(autocompleteCancel());
      return;
    }

    if (value.length > SEARCH_TERM_MAX_LENGTH) {
      log.debug(oneLine`Ignoring suggestions fetch because query
        exceeds max length (${SEARCH_TERM_MAX_LENGTH})`);

      this.props.dispatch(autocompleteCancel());
      return;
    }

    const filters = this.createFiltersFromQuery(value);

    this.setState({ autocompleteIsOpen: true });

    this.dispatchAutocompleteStart({ filters });
  };
  it('cancels the fetch saga when receiving AUTOCOMPLETE_CANCELLED', async () => {
    mockApi
      .expects('autocomplete')
      .once()
      // Add a delay to the API call so that it slows down the fetch saga,
      // allowing the `autocompleteCancel()` to be handled. The delay does not
      // really matter since cancellation is expected as soon as
      // AUTOCOMPLETE_CANCELLED is fired.
      .returns(new Promise((resolve) => setTimeout(resolve, 500)));

    _autocompleteStart({ filters: {} });
    sagaTester.dispatch(autocompleteCancel());

    const expectedCancelAction = autocompleteCancel();

    await sagaTester.waitFor(expectedCancelAction.type);
    mockApi.verify();

    const cancelAction = sagaTester.getCalledActions()[2];
    expect(cancelAction).toEqual(expectedCancelAction);
  });
  it('can call the API for suggestions even after a cancellation', async () => {
    const results = [createFakeAutocompleteResult()];
    const filters = { query: 'test' };

    const autocompleteApi = mockApi.expects('autocomplete').twice();

    // This configures the API for the first autocomplete start.
    autocompleteApi
      .onCall(0)
      // Add a delay to the API call so that it slows down the fetch saga,
      // allowing the `autocompleteCancel()` to be handled. The delay does not
      // really matter since cancellation is expected as soon as
      // AUTOCOMPLETE_CANCELLED is fired.
      .returns(new Promise((resolve) => setTimeout(resolve, 500)));

    // This configures the API for the second autocomplete start.
    autocompleteApi.onCall(1).returns(Promise.resolve({ results }));

    // We start autocompletion, but then cancel it.
    _autocompleteStart({ filters });
    sagaTester.dispatch(autocompleteCancel());

    const expectedCancelAction = autocompleteCancel();
    await sagaTester.waitFor(expectedCancelAction.type);
    const cancelAction = sagaTester.getCalledActions()[2];
    expect(cancelAction).toEqual(expectedCancelAction);

    sagaTester.reset(true);

    // We start autocompletion and let it finish.
    _autocompleteStart({ filters });

    const expectedLoadAction = autocompleteLoad({ results });
    await sagaTester.waitFor(expectedLoadAction.type);
    const loadAction = sagaTester.getCalledActions()[2];
    expect(loadAction).toEqual(expectedLoadAction);

    mockApi.verify();
  });
Example #4
0
 handleSuggestionsClearRequested = () => {
   this.setState({ autocompleteIsOpen: false });
   this.props.dispatch(autocompleteCancel());
 };