Skip to content

Commit

Permalink
fix: deepsourse
Browse files Browse the repository at this point in the history
  • Loading branch information
sherzod-bakhodirov committed Dec 17, 2024
1 parent a255c10 commit 7ed6806
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 55 deletions.
12 changes: 10 additions & 2 deletions packages/@magic-sdk/provider/test/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,16 @@ export function restoreSDKEnvironmentConstants() {
jest.unmock('../src/core/sdk-environment');
}

export function mockLocalForage(FAKE_STORE: Record<string, any> = {}) {
jest.spyOn(storage, 'getItem').mockImplementation((key: string) => FAKE_STORE[key]);
export function mockLocalForage(FAKE_STORE: Record<string, unknown> = {}) {
jest.spyOn(storage, 'getItem').mockImplementation((key: string, callback?: (err: unknown, value: unknown) => void) => {
const value = FAKE_STORE[key];

if (callback) {
callback(null, value);
}

return Promise.resolve(value);
});
jest.spyOn(storage, 'setItem').mockImplementation(async (key: string, value: any) => {
FAKE_STORE[key] = value;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ test('Initialize MagicRPCError with `undefined` argument', () => {

test('Initialize MagicRPCError with unknown error code argument', () => {
const err = new MagicRPCError({
// @ts-ignore
code: 1,
code: -32603,
message: 'hello world',
data: exampleData,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const FAKE_JWT_TOKEN = 'hot tokens';
const FAKE_DEVICE_SHARE = 'fake device share';
const FAKE_RT = 'will freshen';
const FAKE_INJECTED_JWT = 'fake injected jwt';
let FAKE_STORE: Record<string, any> = {};
let FAKE_STORE: Record<string, string> = {};

let viewController: TestViewController;

Expand All @@ -78,9 +78,17 @@ beforeEach(() => {
createJwtStub = jest.spyOn(webCryptoUtils, 'createJwt');
getDecryptedDeviceShareStub = jest.spyOn(deviceShareWebCryptoUtils, 'getDecryptedDeviceShare');
clearDeviceSharesStub = jest.spyOn(deviceShareWebCryptoUtils, 'clearDeviceShares');
jest.spyOn(global.console, 'info').mockImplementation(() => {});
jest.spyOn(global.console, 'info').mockImplementation(() => { });
jest.spyOn(global, 'addEventListener').mockImplementation(jest.fn());
jest.spyOn(storage, 'getItem').mockImplementation((key: string) => FAKE_STORE[key]);
jest.spyOn(storage, 'getItem').mockImplementation((key: string, callback?: (err: unknown, value: unknown) => void) => {
const value = FAKE_STORE[key];

if (callback) {
callback(null, value);
}

return Promise.resolve(value);
});
jest.spyOn(storage, 'setItem').mockImplementation(async (key: string, value: any) => {
FAKE_STORE[key] = value;
});
Expand Down Expand Up @@ -205,7 +213,7 @@ test('Sends payload without rt if no jwt can be made', async () => {
});

test('Sends payload when web crypto jwt fails', async () => {
const consoleErrorStub = jest.spyOn(global.console, 'error').mockImplementationOnce(() => {});
const consoleErrorStub = jest.spyOn(global.console, 'error').mockImplementationOnce(() => { });
createJwtStub.mockRejectedValueOnce('danger');
FAKE_STORE.rt = FAKE_RT;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test('If no parameters are given & platform target is "web", URL search string a
const magic = createMagicSDK({ platform: 'web' });
magic.auth.request = jest.fn();

jest.spyOn(window.history, 'replaceState').mockImplementation(() => { });
jest.spyOn(window.history, 'replaceState').mockImplementation(() => { /* noop */ });

jest.spyOn(window, 'location', 'get').mockReturnValue({
search: '?magic_credential=asdf',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const expectedEmail = 'john.doe@mail.com';

test('Generates JSON RPC pending for otp-input-sent', async () => {
const magic = createMagicSDK();
magic.auth.overlay.post = jest.fn().mockImplementation(() => new Promise(() => {}));
magic.auth.overlay.post = jest.fn().mockImplementation(() => new Promise(() => { /* noop */ }));
const createIntermediaryEventFn = jest.fn();
magic.auth.createIntermediaryEvent = jest.fn().mockImplementation(() => createIntermediaryEventFn);

Expand All @@ -29,7 +29,7 @@ test('Generates JSON RPC pending for otp-input-sent', async () => {

test('Generates JSON RPC pending for verify-mfa-code', () => {
const magic = createMagicSDK();
magic.auth.overlay.post = jest.fn().mockImplementation(() => new Promise(() => {}));
magic.auth.overlay.post = jest.fn().mockImplementation(() => new Promise(() => { /* noop */ }));
const createIntermediaryEventFn = jest.fn();
magic.auth.createIntermediaryEvent = jest.fn().mockImplementation(() => createIntermediaryEventFn);

Expand All @@ -49,7 +49,7 @@ test('Generates JSON RPC pending for verify-mfa-code', () => {

test('Generates JSON RPC pending for lost-device', () => {
const magic = createMagicSDK();
magic.auth.overlay.post = jest.fn().mockImplementation(() => new Promise(() => {}));
magic.auth.overlay.post = jest.fn().mockImplementation(() => new Promise(() => { /* noop */ }));
const createIntermediaryEventFn = jest.fn();
magic.auth.createIntermediaryEvent = jest.fn().mockImplementation(() => createIntermediaryEventFn);

Expand All @@ -67,7 +67,7 @@ test('Generates JSON RPC pending for lost-device', () => {

test('Generates JSON RPC pending for verify-recovery-code', () => {
const magic = createMagicSDK();
magic.auth.overlay.post = jest.fn().mockImplementation(() => new Promise(() => {}));
magic.auth.overlay.post = jest.fn().mockImplementation(() => new Promise(() => { /* noop */ }));
const createIntermediaryEventFn = jest.fn();
magic.auth.createIntermediaryEvent = jest.fn().mockImplementation(() => createIntermediaryEventFn);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const expectedPhoneNumber = 'hey hey I am a number but jk';
test('Generates JSON RPC request payload with `phone` parameter', async () => {
const magic = createMagicSDK();
magic.auth.request = jest.fn();
magic.auth.overlay.post = jest.fn().mockImplementation(() => new Promise(() => {}));
magic.auth.overlay.post = jest.fn().mockImplementation(() => new Promise(() => { /* noop */ }));

await magic.auth.loginWithSMS({ phoneNumber: expectedPhoneNumber, showUI: false });

Expand All @@ -25,7 +25,7 @@ test('Generates JSON RPC request payload with `phone` parameter', async () => {

test('If `testMode` is enabled, testing-specific RPC method is used', async () => {
const magic = createMagicSDKTestMode();
magic.auth.overlay.post = jest.fn().mockImplementation(() => new Promise(() => {}));
magic.auth.overlay.post = jest.fn().mockImplementation(() => new Promise(() => { /* noop */ }));
magic.auth.request = jest.fn();

await magic.auth.loginWithSMS({ phoneNumber: expectedPhoneNumber, showUI: false });
Expand All @@ -39,7 +39,7 @@ test('If `testMode` is enabled, testing-specific RPC method is used', async () =

test('Generates JSON RPC pending for otp-input-sent', () => {
const magic = createMagicSDK();
magic.auth.overlay.post = jest.fn().mockImplementation(() => new Promise(() => {}));
magic.auth.overlay.post = jest.fn().mockImplementation(() => new Promise(() => { /* noop */ }));
const createIntermediaryEventFn = jest.fn();
magic.auth.createIntermediaryEvent = jest.fn().mockImplementation(() => createIntermediaryEventFn);

Expand All @@ -59,7 +59,7 @@ test('Generates JSON RPC pending for otp-input-sent', () => {

test('method should return a PromiEvent', () => {
const magic = createMagicSDK();
magic.auth.overlay.post = jest.fn().mockImplementation(() => new Promise(() => {}));
magic.auth.overlay.post = jest.fn().mockImplementation(() => new Promise(() => { /* noop */ }));

expect(isPromiEvent(magic.auth.loginWithSMS({ email: 'blag' }))).toBeTruthy();
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ beforeEach(() => {

test('Generate JSON RPC request payload with method `magic_auth_update_email` whitelabel and start recency check', async () => {
const magic = createMagicSDK();
magic.auth.overlay.post = jest.fn().mockImplementation(() => new Promise(() => {}));
magic.auth.overlay.post = jest.fn().mockImplementation(() => new Promise(() => { /* noop */ }));
const createIntermediaryEventFn = jest.fn();
magic.auth.createIntermediaryEvent = jest.fn().mockImplementation(() => createIntermediaryEventFn);

Expand All @@ -27,7 +27,7 @@ test('Generate JSON RPC request payload with method `magic_auth_update_email` wh

test('Whitelabel `magic_auth_update_email`, recency check Retry event', async () => {
const magic = createMagicSDK();
magic.auth.overlay.post = jest.fn().mockImplementation(() => new Promise(() => {}));
magic.auth.overlay.post = jest.fn().mockImplementation(() => new Promise(() => { /* noop */ }));
const createIntermediaryEventFn = jest.fn();
magic.auth.createIntermediaryEvent = jest.fn().mockImplementation(() => createIntermediaryEventFn);

Expand All @@ -41,7 +41,7 @@ test('Whitelabel `magic_auth_update_email`, recency check Retry event', async ()

test('Whitelabel `magic_auth_update_email`, Update Email, fire retry with Email event', async () => {
const magic = createMagicSDK();
magic.auth.overlay.post = jest.fn().mockImplementation(() => new Promise(() => {}));
magic.auth.overlay.post = jest.fn().mockImplementation(() => new Promise(() => { /* noop */ }));
const createIntermediaryEventFn = jest.fn();
magic.auth.createIntermediaryEvent = jest.fn().mockImplementation(() => createIntermediaryEventFn);

Expand All @@ -58,7 +58,7 @@ test('Whitelabel `magic_auth_update_email`, Update Email, fire retry with Email

test('Generate JSON RPC request payload with method `magic_auth_update_email` whitelabel and start verify Email otp ', async () => {
const magic = createMagicSDK();
magic.auth.overlay.post = jest.fn().mockImplementation(() => new Promise(() => {}));
magic.auth.overlay.post = jest.fn().mockImplementation(() => new Promise(() => { /* noop */ }));
const createIntermediaryEventFn = jest.fn();
magic.auth.createIntermediaryEvent = jest.fn().mockImplementation(() => createIntermediaryEventFn);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const requestPayload: JsonRpcRequestPayload = {
beforeEach(() => {
jest.restoreAllMocks();
// Silence the "duplicate iframes" warning.
jest.spyOn(console, 'warn').mockImplementation(() => {});
jest.spyOn(console, 'warn').mockImplementation(() => { /* noop */ });
});

test('Resolves with a successful response', async () => {
Expand Down Expand Up @@ -113,7 +113,7 @@ test('Receive no further events after the response from `ViewController` resolve

const request = baseModule
.request(requestPayload)
.on('hello_b', (result:string) => {
.on('hello_b', (result: string) => {
expect(result).toBe('world');
})
.on('hello_b2', () => {
Expand Down Expand Up @@ -159,7 +159,7 @@ test('Falls back to empty array if `params` is missing from event', done => {
),
);

baseModule.request(requestPayload).on('hello_c', (...args:[]) => {
baseModule.request(requestPayload).on('hello_c', (...args: []) => {
expect(args).toEqual([]);
done();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ test('method should return a PromiEvent', () => {

test('method should create intermediary event on cancel', () => {
const magic = createMagicSDK();
magic.user.overlay.post = jest.fn().mockImplementation(() => new Promise(() => {}));
magic.user.overlay.post = jest.fn().mockImplementation(() => new Promise(() => { /* noop */ }));
const createIntermediaryEventFn = jest.fn();
magic.user.createIntermediaryEvent = jest.fn().mockImplementation(() => createIntermediaryEventFn);

Expand All @@ -50,7 +50,7 @@ test('method should create intermediary event on cancel', () => {

test('method should create intermediary event on ResendSms', () => {
const magic = createMagicSDK();
magic.user.overlay.post = jest.fn().mockImplementation(() => new Promise(() => {}));
magic.user.overlay.post = jest.fn().mockImplementation(() => new Promise(() => { /* noop */ }));
const createIntermediaryEventFn = jest.fn();
magic.user.createIntermediaryEvent = jest.fn().mockImplementation(() => createIntermediaryEventFn);

Expand All @@ -64,7 +64,7 @@ test('method should create intermediary event on ResendSms', () => {

test('method should create intermediary event on VerifyOtp', () => {
const magic = createMagicSDK();
magic.user.overlay.post = jest.fn().mockImplementation(() => new Promise(() => {}));
magic.user.overlay.post = jest.fn().mockImplementation(() => new Promise(() => { /* noop */ }));
const createIntermediaryEventFn = jest.fn();
magic.user.createIntermediaryEvent = jest.fn().mockImplementation(() => createIntermediaryEventFn);

Expand All @@ -78,7 +78,7 @@ test('method should create intermediary event on VerifyOtp', () => {

test('method should create intermediary event on UpdateEmail', () => {
const magic = createMagicSDK();
magic.user.overlay.post = jest.fn().mockImplementation(() => new Promise(() => {}));
magic.user.overlay.post = jest.fn().mockImplementation(() => new Promise(() => { /* noop */ }));
const createIntermediaryEventFn = jest.fn();
magic.user.createIntermediaryEvent = jest.fn().mockImplementation(() => createIntermediaryEventFn);

Expand All @@ -92,7 +92,7 @@ test('method should create intermediary event on UpdateEmail', () => {

test('method should create intermediary event on UpdateEmailEventEmit.Cancel', () => {
const magic = createMagicSDK();
magic.user.overlay.post = jest.fn().mockImplementation(() => new Promise(() => {}));
magic.user.overlay.post = jest.fn().mockImplementation(() => new Promise(() => { /* noop */ }));
const createIntermediaryEventFn = jest.fn();
magic.user.createIntermediaryEvent = jest.fn().mockImplementation(() => createIntermediaryEventFn);

Expand All @@ -106,7 +106,7 @@ test('method should create intermediary event on UpdateEmailEventEmit.Cancel', (

test('method should create intermediary event on RetryWithNewEmail', () => {
const magic = createMagicSDK();
magic.user.overlay.post = jest.fn().mockImplementation(() => new Promise(() => {}));
magic.user.overlay.post = jest.fn().mockImplementation(() => new Promise(() => { /* noop */ }));
const createIntermediaryEventFn = jest.fn();
magic.user.createIntermediaryEvent = jest.fn().mockImplementation(() => createIntermediaryEventFn);

Expand All @@ -120,7 +120,7 @@ test('method should create intermediary event on RetryWithNewEmail', () => {

test('method should create intermediary event on VerifyEmailOtp', () => {
const magic = createMagicSDK();
magic.user.overlay.post = jest.fn().mockImplementation(() => new Promise(() => {}));
magic.user.overlay.post = jest.fn().mockImplementation(() => new Promise(() => { /* noop */ }));
const createIntermediaryEventFn = jest.fn();
magic.user.createIntermediaryEvent = jest.fn().mockImplementation(() => createIntermediaryEventFn);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
getDecryptedDeviceShare,
} from '../../../src/util/device-share-web-crypto';

let FAKE_STORE: Record<string, any> = {};
let FAKE_STORE: Record<string, unknown> = {};
const FAKE_NETWORK_HASH = 'network_hash';

const FAKE_PLAINTEXT_SHARE = `AQICAHg1y7j1UY7sfTib6h9cN2Kh7v0WhCRwQxEPhGAQ2m5OgQGrJvUP6MKiuj9yD96y6B4eAAABPzCCATsGCSqGSIb3DQEHBqCCASwwggEoAgEAMIIBIQYJKoZIhvcNAQcBMB4GCWCGSAFlAwQBLjARBAy6tbGg/6//2IJs9xUCARCAgfOY3knm1i2kGjLXQFoqEjOeLr/UGwHQ+AW1y20UoCX3ght68egu06Hg54JF/mCGgSDt7R7dFSOuGvapE9OEyFYz4f1+tpWb5PPaLReBRTTTfw/8Xgsfzl6iXACsLKqyXEeWci+/vOWDLqu73E0uy5StyN5InZLwHCJe4l+KMEr5C7JZvobQh4NVBT5SqgQXmLGXGGH/2ydkq8zkgVGDT9jQlqqpUH83UMFQwHSwbJRRyYLxBwQKTO0AODfqk5OnWRA+BoDC8HMFyQUb4nS+BgDlgTgL7Kg/H/Echr+SlQKJdWJnvf3BjSBwO8z5kVpxRo5xwG4=`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createPromiEvent } from '../../../../src/util/promise-tools';
import { createPromiEvent, PromiEvent } from '../../../../src/util/promise-tools';
import { TypedEmitter } from '../../../../src/util/events';

type DefaultEvents<TResult> = {
Expand All @@ -19,50 +19,50 @@ beforeEach(() => {
});

test('Creates a native `Promise`', () => {
const p = createPromiEvent(resolve => resolve(true));
const promiEvent = createPromiEvent(resolve => resolve(true));

expect(p instanceof Promise).toBe(true);
expect(promiEvent instanceof Promise).toBe(true);
});

test('Attaches `TypedEmitter` methods to the initial value', () => {
const p = createPromiEvent(resolve => resolve(true));
const promiEvent = createPromiEvent(resolve => resolve(true));

typedEmitterMethods.forEach(method => {
expect(typeof p[method as keyof typeof p] === 'function').toBe(true);
expect(typeof promiEvent[method as keyof typeof promiEvent] === 'function').toBe(true);
});
});

test('Attaches `TypedEmitter` methods to `Promise.then` result', () => {
const p = createPromiEvent(resolve => resolve(true)).then();
const promiEvent = createPromiEvent(resolve => resolve(true)).then();

typedEmitterMethods.forEach(method => {
expect(typeof p[method as keyof typeof p] === 'function').toBe(true);
expect(typeof promiEvent[method as keyof typeof promiEvent] === 'function').toBe(true);
});
});

test('Attaches `TypedEmitter` methods to `Promise.catch` result', () => {
const p = createPromiEvent(resolve => resolve(true)).catch();
const promiEvent = createPromiEvent(resolve => resolve(true)).catch();

typedEmitterMethods.forEach(method => {
expect(typeof p[method as keyof typeof p] === 'function').toBe(true);
expect(typeof promiEvent[method as keyof typeof promiEvent] === 'function').toBe(true);
});
});

test('Attaches `TypedEmitter` methods to `Promise.finally` result', () => {
const p = createPromiEvent(resolve => resolve(true)).catch();
const promiEvent = createPromiEvent(resolve => resolve(true)).catch();

typedEmitterMethods.forEach(method => {
expect(typeof p[method as keyof typeof p] === 'function').toBe(true);
expect(typeof promiEvent[method as keyof typeof promiEvent] === 'function').toBe(true);
});
});

test('Attaches `Promise` methods to `TypedEmitter` results', () => {
chainingEmitterMethods.forEach(emitterMethod => {
const emitterStub = jest.spyOn(TypedEmitter.prototype, emitterMethod as any).mockImplementation();
const p = createPromiEvent(resolve => resolve(true))[emitterMethod]('done', () => {});
const promiEvent = createPromiEvent(resolve => resolve(true))[emitterMethod]('done', () => { /* noop */ });

promiseMethods.forEach(promiseMethod => {
expect(typeof (p as Record<string, any>)[promiseMethod] === 'function').toBe(true);
expect(typeof promiEvent[promiseMethod as keyof typeof promiEvent] === 'function').toBe(true);
});

emitterStub.mockReset();
Expand All @@ -89,13 +89,17 @@ test('Emits "error" event upon Promise reject', done => {
expect(err).toBe('goodbye' as any);
done();
})
.catch(() => { });
.catch(() => {
/* noop */
});
});

test('Emits "settled" event upon Promise reject', done => {
createPromiEvent((resolve, reject) => reject())
.on('settled', () => {
done();
})
.catch(() => { });
.catch(() => {
/* noop */
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ beforeEach(() => {
});

test('Creates a native `Promise`', () => {
const p = createPromise(resolve => resolve(true));
const promiEvent = createPromise(resolve => resolve(true));

expect(p instanceof Promise).toBe(true);
expect(promiEvent instanceof Promise).toBe(true);
});

test('Resolves the `Promise`', done => {
Expand Down
Loading

0 comments on commit 7ed6806

Please sign in to comment.