-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not highlight matched asymmetricMatcher in diffs (#9257)
- Loading branch information
Showing
9 changed files
with
790 additions
and
10 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
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,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); | ||
}); | ||
} 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; | ||
} | ||
} | ||
} |
154 changes: 154 additions & 0 deletions
154
packages/jest-matcher-utils/src/__tests__/Replaceable.test.ts
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,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); | ||
}); | ||
}); | ||
}); |
155 changes: 155 additions & 0 deletions
155
packages/jest-matcher-utils/src/__tests__/__snapshots__/printDiffOrStringify.test.ts.snap
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
Oops, something went wrong.