-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Do not highlight matched asymmetricMatcher in diffs #9257
Changes from all commits
586b17a
ec68d66
4f7c31a
e69b816
727d5e5
2c88e99
3885f56
7a04cb0
48ab232
69fbc5d
149783b
883f4cd
a6632ed
4f5c1a9
29154cd
e99e985
7aa6f27
b148c0f
fd6443e
ba5d7ec
34c18dc
e0a6288
c5361b4
c90a4ed
d7c9a55
b438d6d
65bd0cc
61f2c9f
4c0f959
03e00c3
9a81ffc
67c443e
263f9a7
d5863fa
8f5321a
83ddb57
4d4b7ce
d9dd2c4
d4c2f5e
1db1b73
739d835
3e200ce
829ef89
684b8ff
0a26b71
0f441f4
c63cc72
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import getType = require('jest-get-type'); | ||
|
||
const supportTypes = ['map', 'array', 'object']; | ||
|
||
type ReplaceableForEachCallBack = (value: any, key: any, object: any) => void; | ||
|
||
export default class Replaceable { | ||
object: any; | ||
type: string; | ||
|
||
constructor(object: any) { | ||
this.object = object; | ||
this.type = getType(object); | ||
if (!supportTypes.includes(this.type)) { | ||
throw new Error(`Type ${this.type} is not support in Replaceable!`); | ||
} | ||
} | ||
|
||
static isReplaceable(obj1: any, obj2: any): boolean { | ||
const obj1Type = getType(obj1); | ||
const obj2Type = getType(obj2); | ||
return obj1Type === obj2Type && supportTypes.includes(obj1Type); | ||
} | ||
|
||
forEach(cb: ReplaceableForEachCallBack): void { | ||
if (this.type === 'object') { | ||
Object.entries(this.object).forEach(([key, value]) => { | ||
cb(value, key, this.object); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
See Therefore, you might need to add the following iteration for enumerable symbol-keyed properties: Object.getOwnPropertySymbols(this.object).forEach(key => {
const descriptor = Object.getOwnPropertyDescriptor(this.object, key);
if ((descriptor as PropertyDescriptor).enumerable) {
cb(this.object[key], key, this.object);
}
}); The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. None of the diff snapshots in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will add some symbol keys test cases, and make them work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @WeiAnAn For your information, the current target for Jest 25 is Tuesday January 21, so if this rare edge case will slow you down too much, please leave it for a follow-up pull request. The copy function is the important thing. |
||
} else { | ||
this.object.forEach(cb); | ||
} | ||
} | ||
|
||
get(key: any): any { | ||
if (this.type === 'map') { | ||
return this.object.get(key); | ||
} | ||
return this.object[key]; | ||
} | ||
|
||
set(key: any, value: any): void { | ||
if (this.type === 'map') { | ||
this.object.set(key, value); | ||
} else { | ||
this.object[key] = value; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import Replaceable from '../Replaceable'; | ||
|
||
describe('Replaceable', () => { | ||
describe('constructor', () => { | ||
test('init with object', () => { | ||
const replaceable = new Replaceable({a: 1, b: 2}); | ||
expect(replaceable.object).toEqual({a: 1, b: 2}); | ||
expect(replaceable.type).toBe('object'); | ||
}); | ||
|
||
test('init with array', () => { | ||
const replaceable = new Replaceable([1, 2, 3]); | ||
expect(replaceable.object).toEqual([1, 2, 3]); | ||
expect(replaceable.type).toBe('array'); | ||
}); | ||
|
||
test('init with Map', () => { | ||
const replaceable = new Replaceable( | ||
new Map([ | ||
['a', 1], | ||
['b', 2], | ||
]), | ||
); | ||
expect(replaceable.object).toEqual( | ||
new Map([ | ||
['a', 1], | ||
['b', 2], | ||
]), | ||
); | ||
expect(replaceable.type).toBe('map'); | ||
}); | ||
|
||
test('init with other type should throw error', () => { | ||
expect(() => { | ||
//eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const replaceable = new Replaceable(new Date()); | ||
}).toThrow('Type date is not support in Replaceable!'); | ||
}); | ||
}); | ||
|
||
describe('get', () => { | ||
test('get object item', () => { | ||
const replaceable = new Replaceable({a: 1, b: 2}); | ||
expect(replaceable.get('b')).toBe(2); | ||
}); | ||
|
||
test('get array item', () => { | ||
const replaceable = new Replaceable([1, 2, 3]); | ||
expect(replaceable.get(1)).toBe(2); | ||
}); | ||
|
||
test('get Map item', () => { | ||
const replaceable = new Replaceable( | ||
new Map([ | ||
['a', 1], | ||
['b', 2], | ||
]), | ||
); | ||
expect(replaceable.get('b')).toBe(2); | ||
}); | ||
}); | ||
|
||
describe('set', () => { | ||
test('set object item', () => { | ||
const replaceable = new Replaceable({a: 1, b: 2}); | ||
replaceable.set('b', 3); | ||
expect(replaceable.object).toEqual({a: 1, b: 3}); | ||
}); | ||
|
||
test('set array item', () => { | ||
const replaceable = new Replaceable([1, 2, 3]); | ||
replaceable.set(1, 3); | ||
expect(replaceable.object).toEqual([1, 3, 3]); | ||
}); | ||
|
||
test('set Map item', () => { | ||
const replaceable = new Replaceable( | ||
new Map([ | ||
['a', 1], | ||
['b', 2], | ||
]), | ||
); | ||
replaceable.set('b', 3); | ||
expect(replaceable.object).toEqual( | ||
new Map([ | ||
['a', 1], | ||
['b', 3], | ||
]), | ||
); | ||
}); | ||
}); | ||
|
||
describe('forEach', () => { | ||
test('object forEach', () => { | ||
const replaceable = new Replaceable({a: 1, b: 2}); | ||
const cb = jest.fn(); | ||
replaceable.forEach(cb); | ||
expect(cb.mock.calls[0]).toEqual([1, 'a', {a: 1, b: 2}]); | ||
expect(cb.mock.calls[1]).toEqual([2, 'b', {a: 1, b: 2}]); | ||
}); | ||
|
||
test('array forEach', () => { | ||
const replaceable = new Replaceable([1, 2, 3]); | ||
const cb = jest.fn(); | ||
replaceable.forEach(cb); | ||
expect(cb.mock.calls[0]).toEqual([1, 0, [1, 2, 3]]); | ||
expect(cb.mock.calls[1]).toEqual([2, 1, [1, 2, 3]]); | ||
expect(cb.mock.calls[2]).toEqual([3, 2, [1, 2, 3]]); | ||
}); | ||
|
||
test('map forEach', () => { | ||
const map = new Map([ | ||
['a', 1], | ||
['b', 2], | ||
]); | ||
const replaceable = new Replaceable(map); | ||
const cb = jest.fn(); | ||
replaceable.forEach(cb); | ||
expect(cb.mock.calls[0]).toEqual([1, 'a', map]); | ||
expect(cb.mock.calls[1]).toEqual([2, 'b', map]); | ||
}); | ||
}); | ||
|
||
describe('isReplaceable', () => { | ||
test('should return true if two object types equal and support', () => { | ||
expect(Replaceable.isReplaceable({a: 1}, {b: 2})).toBe(true); | ||
expect(Replaceable.isReplaceable([], [1, 2, 3])).toBe(true); | ||
expect( | ||
Replaceable.isReplaceable( | ||
new Map(), | ||
new Map([ | ||
['a', 1], | ||
['b', 2], | ||
]), | ||
), | ||
).toBe(true); | ||
}); | ||
|
||
test('should return false if two object types not equal', () => { | ||
expect(Replaceable.isReplaceable({a: 1}, [1, 2, 3])).toBe(false); | ||
}); | ||
|
||
test('should return false if object types not support', () => { | ||
expect(Replaceable.isReplaceable('foo', 'bar')).toBe(false); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can all these
any
s beunknown
s?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If change
any
tounknown
, it will do a lot task for type converting.For example:
I think it make code a little bit ugly, although is suitable for typescript.