diff options
author | Sebastian <sebasjm@gmail.com> | 2023-01-02 15:11:28 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2023-01-03 01:58:17 -0300 |
commit | d1aa79eae817b1cf4c23f800308ecad101692ac7 (patch) | |
tree | 32282462d6dc6116ffe627159286f31a1494c7b1 /packages/web-util/src/tests/mock.ts | |
parent | d48ea17c6345abaa732deb5fab6141449d463e41 (diff) | |
download | wallet-core-d1aa79eae817b1cf4c23f800308ecad101692ac7.tar.xz |
remove axios, better api
Diffstat (limited to 'packages/web-util/src/tests/mock.ts')
-rw-r--r-- | packages/web-util/src/tests/mock.ts | 46 |
1 files changed, 38 insertions, 8 deletions
diff --git a/packages/web-util/src/tests/mock.ts b/packages/web-util/src/tests/mock.ts index 563e437e5..115994cbd 100644 --- a/packages/web-util/src/tests/mock.ts +++ b/packages/web-util/src/tests/mock.ts @@ -48,8 +48,10 @@ type ExpectationValues = { query: Query<any, any>; auth?: string; params?: { + // eslint-disable-next-line @typescript-eslint/ban-types request?: object; qparam?: Record<string, string>; + // eslint-disable-next-line @typescript-eslint/ban-types response?: object; }; }; @@ -59,7 +61,7 @@ type TestValues = { lastQuery: ExpectationValues | undefined; }; -const logger = new Logger("testing/swr.ts"); +const logger = new Logger("testing/mock.ts"); export abstract class MockEnvironment { expectations: Array<ExpectationValues> = []; @@ -69,11 +71,13 @@ export abstract class MockEnvironment { debug: boolean; constructor(debug: boolean) { this.debug = debug; - this.registerRequest.bind(this); + this.saveRequestAndGetMockedResponse.bind(this); } public addRequestExpectation< + // eslint-disable-next-line @typescript-eslint/ban-types RequestType extends object, + // eslint-disable-next-line @typescript-eslint/ban-types ResponseType extends object, >( query: Query<RequestType, ResponseType>, @@ -89,13 +93,12 @@ export abstract class MockEnvironment { if (this.debug) { logger.info("saving query as expected", expected); } - this.mockApiIfNeeded(); } - abstract mockApiIfNeeded(): void; - - public registerRequest< + public saveRequestAndGetMockedResponse< + // eslint-disable-next-line @typescript-eslint/ban-types RequestType extends object, + // eslint-disable-next-line @typescript-eslint/ban-types ResponseType extends object, >( query: Query<RequestType, ResponseType>, @@ -167,8 +170,8 @@ export abstract class MockEnvironment { }; } - private assertNextRequest(idx: number): AssertStatus { - const { currentExpectedQuery, lastQuery } = this.getLastTestValues(idx); + private assertNextRequest(index: number): AssertStatus { + const { currentExpectedQuery, lastQuery } = this.getLastTestValues(index); if (!currentExpectedQuery) { return { @@ -194,6 +197,9 @@ export abstract class MockEnvironment { return { result: "error-difference", diff: "url", + last: lastQuery.query.url, + expected: currentExpectedQuery.query.url, + index, }; } } @@ -206,6 +212,9 @@ export abstract class MockEnvironment { return { result: "error-difference", diff: "query-body", + expected: currentExpectedQuery.params?.request, + last: lastQuery.params?.request, + index, }; } if ( @@ -214,12 +223,18 @@ export abstract class MockEnvironment { return { result: "error-difference", diff: "query-params", + expected: currentExpectedQuery.params?.qparam, + last: lastQuery.params?.qparam, + index, }; } if (!deepEquals(currentExpectedQuery.auth, lastQuery.auth)) { return { result: "error-difference", diff: "query-auth", + expected: currentExpectedQuery.auth, + last: lastQuery.auth, + index, }; } @@ -269,18 +284,30 @@ interface AssertExpectedQueryMethodMismatch { interface AssertExpectedQueryUrlMismatch { result: "error-difference"; diff: "url"; + last: string; + expected: string; + index: number; } interface AssertExpectedQueryAuthMismatch { result: "error-difference"; diff: "query-auth"; + last: string | undefined; + expected: string | undefined; + index: number; } interface AssertExpectedQueryBodyMismatch { result: "error-difference"; diff: "query-body"; + last: any; + expected: any; + index: number; } interface AssertExpectedQueryParamsMismatch { result: "error-difference"; diff: "query-params"; + last: any; + expected: any; + index: number; } /** @@ -383,7 +410,9 @@ function deepEquals( let keyIterator: string; while (keySize--) { + // eslint-disable-next-line @typescript-eslint/ban-types const _a = a as Record<string, object>; + // eslint-disable-next-line @typescript-eslint/ban-types const _b = b as Record<string, object>; keyIterator = aKeys[keySize]; @@ -400,6 +429,7 @@ function deepEquals( return true; } +// eslint-disable-next-line @typescript-eslint/ban-types function allKeysFromObject(obj: object): Array<string> { const keys = []; for (const key in obj) { |