diff --git a/tests/unit/RequestTest.js b/tests/unit/RequestTest.ts similarity index 73% rename from tests/unit/RequestTest.js rename to tests/unit/RequestTest.ts index 823b05fd4e0e6..de05f9cab2fac 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(); }); @@ -10,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 = { - command: 'MockCommand', - data: {authToken: 'testToken'}, - }; Request.processWithMiddleware(request, true); return waitForBatchedUpdates().then(() => { @@ -35,11 +39,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; } @@ -50,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(() => {