Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions tests/unit/RequestTest.js → tests/unit/RequestTest.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about migrating TestHelper in this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm migrating TestHelper on another PR, I'll make sure this is removed before I merge the TestHelper one.

global.fetch = TestHelper.getGlobalFetchMock();
});

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(() => {
Expand All @@ -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<void | OnyxTypes.Response>) =>
promise.then(
(response) =>
(response: void | OnyxTypes.Response) =>
new Promise((resolve, reject) => {
if (response.jsonCode !== 404) {
if (typeof response === 'object' && response.jsonCode !== 404) {
return;
}

Expand All @@ -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(() => {
Expand Down