From f19d81e939848b3dc9a944ae78148d7f699b9ff8 Mon Sep 17 00:00:00 2001 From: ruben-rebelo Date: Wed, 21 Feb 2024 09:15:22 +0000 Subject: [PATCH 1/2] [TS migration] Migrate RequestTest to Typescript --- tests/unit/{RequestTest.js => RequestTest.ts} | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) rename tests/unit/{RequestTest.js => RequestTest.ts} (77%) diff --git a/tests/unit/RequestTest.js b/tests/unit/RequestTest.ts similarity index 77% rename from tests/unit/RequestTest.js rename to tests/unit/RequestTest.ts index 823b05fd4e0e6..3ac0b8d9ed6e5 100644 --- a/tests/unit/RequestTest.js +++ b/tests/unit/RequestTest.ts @@ -1,8 +1,11 @@ -import * as Request from '../../src/libs/Request'; +import * as Request from '@src/libs/Request'; +import type {Middleware} from '@src/libs/Request'; +import type * as OnyxTypes from '@src/types/onyx'; import * as TestHelper from '../utils/TestHelper'; import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; beforeAll(() => { + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. global.fetch = TestHelper.getGlobalFetchMock(); }); @@ -13,7 +16,7 @@ beforeEach(() => { test('Request.use() can register a middleware and it will run', () => { const testMiddleware = jest.fn(); Request.use(testMiddleware); - const request = { + const request: OnyxTypes.Request = { command: 'MockCommand', data: {authToken: 'testToken'}, }; @@ -35,11 +38,11 @@ test('Request.use() can register two middlewares. They can pass a response to th }); // And another middleware that will throw when it sees this jsonCode - const errorThrowingMiddleware = (promise) => + const errorThrowingMiddleware: Middleware = (promise: Promise) => promise.then( - (response) => + (response: void | OnyxTypes.Response) => new Promise((resolve, reject) => { - if (response.jsonCode !== 404) { + if (typeof response === 'object' && response.jsonCode !== 404) { return; } From d7d35f5400e9cc7786c03b83a3c97df9842f0a00 Mon Sep 17 00:00:00 2001 From: ruben-rebelo Date: Mon, 4 Mar 2024 09:18:49 +0000 Subject: [PATCH 2/2] [TS migration][RequestTest] Feedback --- tests/unit/RequestTest.ts | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/tests/unit/RequestTest.ts b/tests/unit/RequestTest.ts index 3ac0b8d9ed6e5..de05f9cab2fac 100644 --- a/tests/unit/RequestTest.ts +++ b/tests/unit/RequestTest.ts @@ -13,13 +13,14 @@ beforeEach(() => { Request.clearMiddlewares(); }); +const request: OnyxTypes.Request = { + command: 'MockCommand', + data: {authToken: 'testToken'}, +}; + test('Request.use() can register a middleware and it will run', () => { const testMiddleware = jest.fn(); Request.use(testMiddleware); - const request: OnyxTypes.Request = { - command: 'MockCommand', - data: {authToken: 'testToken'}, - }; Request.processWithMiddleware(request, true); return waitForBatchedUpdates().then(() => { @@ -53,11 +54,6 @@ test('Request.use() can register two middlewares. They can pass a response to th Request.use(testMiddleware); Request.use(errorThrowingMiddleware); - const request = { - command: 'MockCommand', - data: {authToken: 'testToken'}, - }; - const catchHandler = jest.fn(); Request.processWithMiddleware(request).catch(catchHandler); return waitForBatchedUpdates().then(() => {