This repository has been archived by the owner on Oct 7, 2024. It is now read-only.
generated from MetaMask/metamask-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: rename files to be more idiomatic (#42)
* chore: rename files * refactor: move `DeferredPromise` to own file * chore: add unit tests and associated fixes * chore: update comment * chore: add missing return type * chore: increase coverage threshold
- Loading branch information
Showing
8 changed files
with
190 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { DeferredPromise } from './DeferredPromise'; | ||
import { ensureDefined } from './util'; | ||
|
||
describe('DeferredPromise', () => { | ||
it('should not fail to create a DeferredPromise', () => { | ||
expect(() => new DeferredPromise()).not.toThrow(); | ||
}); | ||
|
||
it('should define resolve and reject fields', () => { | ||
const promise = new DeferredPromise(); | ||
expect(promise.resolve).toBeDefined(); | ||
expect(promise.reject).toBeDefined(); | ||
}); | ||
|
||
it('resolves with the correct value', async () => { | ||
const deferred = new DeferredPromise<string>(); | ||
ensureDefined(deferred.resolve); | ||
deferred.resolve('hello'); | ||
expect(await deferred.promise).toBe('hello'); | ||
}); | ||
|
||
it('rejects with the correct reason', async () => { | ||
const deferred = new DeferredPromise<string>(); | ||
ensureDefined(deferred.reject); | ||
deferred.reject('error'); | ||
await expect(deferred.promise).rejects.toBe('error'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* A deferred promise can be resolved by a caller different from the one who | ||
* created it. | ||
* | ||
* Example: | ||
* - "A" creates a deferred promise "P", adds it to a list, and awaits it | ||
* - "B" gets "P" from the list and resolves it | ||
* - "A" gets the resolved value | ||
*/ | ||
export class DeferredPromise<T> { | ||
promise: Promise<T>; | ||
|
||
resolve?: (value: T | PromiseLike<T>) => void; | ||
|
||
reject?: (reason?: any) => void; | ||
|
||
constructor() { | ||
this.promise = new Promise((resolve, reject) => { | ||
this.resolve = resolve; | ||
this.reject = reject; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from './types'; | ||
export * from './snap-keyring'; | ||
export * from './SnapKeyring'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,54 @@ | ||
import { DeferredPromise } from './util'; | ||
|
||
describe('DeferredPromise', () => { | ||
describe('constructor', () => { | ||
it('should not fail to create a DeferredPromise', () => { | ||
expect(() => new DeferredPromise()).not.toThrow(); | ||
}); | ||
|
||
it('should define resolve and reject fields', () => { | ||
const promise = new DeferredPromise(); | ||
expect(promise.resolve).toBeDefined(); | ||
expect(promise.reject).toBeDefined(); | ||
}); | ||
import { ensureDefined, toJson, unique } from './util'; | ||
|
||
describe('unique', () => { | ||
it('returns an empty array when given an empty array', () => { | ||
const arr: number[] = []; | ||
const result = unique(arr); | ||
expect(result).toStrictEqual([]); | ||
}); | ||
|
||
it('returns an array with unique elements', () => { | ||
const arr = [1, 2, 2, 3, 3, 3]; | ||
const result = unique(arr); | ||
expect(result).toStrictEqual([1, 2, 3]); | ||
}); | ||
|
||
it('returns an array with unique objects', () => { | ||
const obj1 = { name: 'John' }; | ||
const obj2 = { name: 'Jane' }; | ||
const arr = [obj1, obj1, obj2]; | ||
const result = unique(arr); | ||
expect(result).toStrictEqual([{ name: 'John' }, { name: 'Jane' }]); | ||
}); | ||
}); | ||
|
||
describe('toJson', () => { | ||
it('correctly serializes an object to JSON', () => { | ||
const obj = { name: 'John', age: 30 }; | ||
const json = toJson(obj); | ||
expect(json).toStrictEqual(obj); | ||
}); | ||
|
||
it('correctly serializes an array to JSON', () => { | ||
const arr = [1, 2, 3]; | ||
const json = toJson(arr); | ||
expect(json).toStrictEqual(arr); | ||
}); | ||
|
||
it('correctly serializes an object with defined and non-undefined fields to JSON', () => { | ||
const obj = { name: 'John', age: undefined }; | ||
const expectedJson = { name: 'John' }; | ||
const json = toJson(obj); | ||
expect(json).toStrictEqual(expectedJson); | ||
}); | ||
}); | ||
|
||
describe('ensureDefined', () => { | ||
it('does not throw an error when value is defined', () => { | ||
expect(() => ensureDefined('hello')).not.toThrow(); | ||
}); | ||
|
||
it('throws an error when value is undefined', () => { | ||
expect(() => ensureDefined(undefined)).toThrow('Argument is undefined'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters