当前位置: 首页>>代码示例>>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;未经允许,请勿转载。