generated from MetaMask/metamask-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
160 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,134 @@ | ||
import { FrozenMap, FrozenSet } from './collections'; | ||
|
||
// describe('FrozenMap', () => { | ||
// it.todo('does the thing'); | ||
// }) | ||
|
||
// describe('FrozenSet', () => { | ||
// it.todo('does the thing'); | ||
// }) | ||
|
||
console.log(Object.isFrozen(FrozenSet)); | ||
console.log(Object.isFrozen(FrozenSet.prototype)); | ||
|
||
const foo = new FrozenSet(['a', 'b', 'c']); | ||
console.log(foo); | ||
console.log(foo.toString()); | ||
console.log(new FrozenSet().toString()); | ||
console.log(new FrozenSet(['a']).toString()); | ||
console.log(new FrozenSet(['a', 'b']).toString()); | ||
console.log(new Set()); | ||
console.log(new Set(['a', 'b', 'c'])); | ||
|
||
console.log(); | ||
console.log(); | ||
|
||
const bar = new FrozenMap([ | ||
['a', 1], | ||
['b', 2], | ||
['c', 3], | ||
]); | ||
console.log(bar); | ||
console.log(bar.toString()); | ||
console.log(new FrozenMap().toString()); | ||
console.log(new FrozenMap([['a', 1]]).toString()); | ||
console.log( | ||
new FrozenMap([ | ||
['a', 1], | ||
['b', 2], | ||
]).entries(), | ||
); | ||
console.log(new Map()); | ||
console.log(new Map([ | ||
['a', 1], | ||
['b', 2], | ||
['c', 3], | ||
])); | ||
describe('FrozenMap', () => { | ||
describe('immutability', () => { | ||
it('is frozen and cannot be mutated', () => { | ||
const frozenMap: any = new FrozenMap(); | ||
expect(frozenMap.set).toBeUndefined(); | ||
expect(frozenMap.clear).toBeUndefined(); | ||
|
||
const expectedProperties = new Set([ | ||
'entries', | ||
'forEach', | ||
'get', | ||
'has', | ||
'keys', | ||
'values', | ||
]); | ||
const properties = Object.getOwnPropertyNames(frozenMap); | ||
expect(properties).toHaveLength(expectedProperties.size); | ||
properties.forEach((propertyName) => | ||
expect(expectedProperties.has(propertyName)).toBe(true), | ||
); | ||
|
||
expect(frozenMap.valueOf() instanceof Map).toBe(false); | ||
|
||
expect(Object.isFrozen(frozenMap)).toBe(true); | ||
expect(Object.isFrozen(frozenMap.prototype)).toBe(true); | ||
|
||
expect(Object.isFrozen(FrozenMap)).toBe(true); | ||
expect(Object.isFrozen(FrozenMap.prototype)).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('iteration', () => { | ||
it('can be used as an iterator', () => { | ||
const input = [ | ||
['a', 1], | ||
['b', 2], | ||
['c', 3], | ||
] as const; | ||
const map = new Map([...input]); | ||
const frozenMap = new FrozenMap([...input]); | ||
|
||
for (const [key, value] of frozenMap) { | ||
expect(map.get(key)).toStrictEqual(value); | ||
} | ||
}); | ||
}); | ||
|
||
describe('toString', () => { | ||
it('stringifies as expected', () => { | ||
expect(new FrozenMap().toString()).toMatchInlineSnapshot( | ||
`"FrozenMap(0) {}"`, | ||
); | ||
|
||
expect(new FrozenMap([['a', 1]]).toString()).toMatchInlineSnapshot( | ||
`"FrozenMap(1) { a => 1 }"`, | ||
); | ||
|
||
expect( | ||
new FrozenMap([ | ||
['a', 1], | ||
['b', 2], | ||
['c', 3], | ||
]).toString(), | ||
).toMatchInlineSnapshot(`"FrozenMap(3) { a => 1, b => 2, c => 3 }"`); | ||
}); | ||
}); | ||
// describe('forEach', () => {}) | ||
// describe('get', () => {}) | ||
// describe('has', () => {}) | ||
// describe('keys', () => {}) | ||
// describe('values', () => {}) | ||
// describe('size', () => {}) | ||
}); | ||
|
||
describe('FrozenSet', () => { | ||
describe('immutability', () => { | ||
it('is frozen and cannot be mutated', () => { | ||
const frozenSet: any = new FrozenSet(); | ||
expect(frozenSet.set).toBeUndefined(); | ||
expect(frozenSet.clear).toBeUndefined(); | ||
|
||
const expectedProperties = new Set([ | ||
'entries', | ||
'forEach', | ||
'has', | ||
'keys', | ||
'values', | ||
]); | ||
const properties = Object.getOwnPropertyNames(frozenSet); | ||
expect(properties).toHaveLength(expectedProperties.size); | ||
properties.forEach((propertyName) => | ||
expect(expectedProperties.has(propertyName)).toBe(true), | ||
); | ||
|
||
expect(frozenSet.valueOf() instanceof Set).toBe(false); | ||
|
||
expect(Object.isFrozen(frozenSet)).toBe(true); | ||
expect(Object.isFrozen(frozenSet.prototype)).toBe(true); | ||
|
||
expect(Object.isFrozen(FrozenSet)).toBe(true); | ||
expect(Object.isFrozen(FrozenSet.prototype)).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('iteration', () => { | ||
it('can be used as an iterator', () => { | ||
const input = ['a', 'b', 'c']; | ||
const set = new Set([...input]); | ||
const frozenSet = new FrozenSet([...input]); | ||
|
||
for (const value of frozenSet) { | ||
expect(set.has(value)).toBe(true); | ||
} | ||
}); | ||
}); | ||
|
||
describe('toString', () => { | ||
it('stringifies as expected', () => { | ||
expect(new FrozenSet().toString()).toMatchInlineSnapshot( | ||
`"FrozenSet(0) {}"`, | ||
); | ||
|
||
expect(new FrozenSet(['a']).toString()).toMatchInlineSnapshot( | ||
`"FrozenSet(1) { a }"`, | ||
); | ||
|
||
expect(new FrozenSet(['a', 'b', 'c']).toString()).toMatchInlineSnapshot( | ||
`"FrozenSet(3) { a, b, c }"`, | ||
); | ||
}); | ||
}); | ||
}); |
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