當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript fetch-mock.mock函數代碼示例

本文整理匯總了TypeScript中src/util/fetch-mock.mock函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript mock函數的具體用法?TypeScript mock怎麽用?TypeScript mock使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了mock函數的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: it

      it("dismisses by setting local and persistent dismissal", function (done) {
        fetchMock.mock({
          matcher: `${API_PREFIX}/uidata`,
          method: "POST",
          response: (_url: string) => {
            const encodedResponse = protos.cockroach.server.serverpb.SetUIDataResponse.encode({}).finish();
            return {
              body: encodedResponse,
            };
          },
        });

        dispatch(setUIDataKey(VERSION_DISMISSED_KEY, null));
        dispatch(versionReducerObj.receiveData({
          details: [
            {
              version: "0.1",
              detail: "alpha",
            },
          ],
        }));
        const alert = newVersionNotificationSelector(state());
        const beforeDismiss = moment();

        dispatch(alert.dismiss).then(() => {
          assert.isTrue(newVersionDismissedLocalSetting.selector(state()).isSameOrAfter(beforeDismiss));
          assert.isNotNull(state().uiData[VERSION_DISMISSED_KEY]);
          assert.isNotNull(state().uiData[VERSION_DISMISSED_KEY].data);
          const dismissedMoment = moment(state().uiData[VERSION_DISMISSED_KEY].data as number);
          assert.isTrue(dismissedMoment.isSameOrAfter(beforeDismiss));
          done();
        });
      });
開發者ID:arjunravinarayan,項目名稱:cockroach,代碼行數:33,代碼來源:alerts.spec.ts

示例2: it

    it("handles missing keys", function () {
      const missingKey = "missingKey";

      const expectedURL = `${api.API_PREFIX}/uidata?keys=${missingKey}`;

      fetchMock.mock({
        matcher: expectedURL,
        response: () => {
          assert.equal(state[missingKey].status, uidata.UIDataStatus.LOADING);

          const encodedResponse = protos.cockroach.server.serverpb.GetUIDataResponse.encode({}).finish();
          return {
            body: api.toArrayBuffer(encodedResponse),
          };
        },
      });

      const p = loadUIData(missingKey);

      return p.then(() => {
        assert.lengthOf(fetchMock.calls(expectedURL), 1);
        assert.lengthOf(_.keys(state), 1);
        assert.equal(state[missingKey].data, undefined);
        assert.property(state[missingKey], "data");
        assert.equal(state[missingKey].status, uidata.UIDataStatus.VALID);
        assert.isNull(state[missingKey].error);
      });
    });
開發者ID:a6802739,項目名稱:cockroach,代碼行數:28,代碼來源:uiData.spec.ts

示例3: beforeEach

    beforeEach(function() {
      // We don't care about the responses, we only care that the sync listener
      // is making requests, which can be verified using "inFlight" settings.
      fetchMock.mock({
        matcher: "*",
        method: "GET",
        response: () => 500,
      });

      sync = alertDataSync(store);
    });
開發者ID:arjunravinarayan,項目名稱:cockroach,代碼行數:11,代碼來源:alerts.spec.ts

示例4: it

    it ("correctly responds to errors.", function () {
      this.timeout(1000);

      // Mock out fetch server; send a positive reply to the first request, and
      // an error to the second request.
      let successSent = false;
      fetchMock.mock({
        matcher: "ts/query",
        method: "POST",
        response: (_url: string, requestObj: RequestInit) => {
          // Assert that metric store's "inFlight" is 1.
          assert.equal(mockMetricsState.inFlight, 1);

          if (successSent) {
            return { throws: new Error() };
          }
          successSent = true;

          const request = protos.cockroach.ts.tspb.TimeSeriesQueryRequest.decode(new Uint8Array(requestObj.body as ArrayBuffer));
          const encodedResponse = protos.cockroach.ts.tspb.TimeSeriesQueryResponse.encode({
            results: _.map(request.queries, (q) => {
              return {
                query: q,
                datapoints: [],
              };
            }),
          }).finish();

          return {
            body: api.toArrayBuffer(encodedResponse),
          };
        },
      });

      // Dispatch several requests. Requests are divided among two timespans,
      // which should result in two batches.
      const shortTimespan: timespan = [Long.fromNumber(400), Long.fromNumber(500)];
      const longTimespan: timespan = [Long.fromNumber(0), Long.fromNumber(500)];
      queryMetrics("id.1", createRequest(shortTimespan, "short.1", "short.2"));
      const p = queryMetrics("id.2", createRequest(longTimespan, "long.1"));

      // Queries should already be present, but unfulfilled.
      assert.lengthOf(_.keys(mockMetricsState.queries), 2);
      _.each(mockMetricsState.queries, (q) => {
        assert.isDefined(q.nextRequest);
        assert.isUndefined(q.data);
      });

      return p.then(() => {
        // Assert that the server got the correct number of requests (2).
        assert.lengthOf(fetchMock.calls("ts/query"), 2);
        // Assert that the mock metrics state has 2 queries.
        assert.lengthOf(_.keys(mockMetricsState.queries), 2);
        // Assert query with id.1 has results.
        const q1 = mockMetricsState.queries["id.1"];
        assert.isDefined(q1);
        assert.isDefined(q1.data);
        assert.isUndefined(q1.error);
        // Assert query with id.2 has an error.
        const q2 = mockMetricsState.queries["id.2"];
        assert.isDefined(q2);
        assert.isDefined(q2.error);
        assert.isUndefined(q2.data);
        // Assert that inFlight is 0.
        assert.equal(mockMetricsState.inFlight, 0);
      });
    });
開發者ID:arjunravinarayan,項目名稱:cockroach,代碼行數:67,代碼來源:metrics.spec.ts


注:本文中的src/util/fetch-mock.mock函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。