-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Update TypeScript example to show use of newer Jest types #10399
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,88 @@ | ||
import Memory from '../memory'; | ||
import sub from '../sub'; | ||
import sum from '../sum'; | ||
import makeCalc from '../calc'; | ||
|
||
jest.mock('../memory'); | ||
jest.mock('../sub'); | ||
jest.mock('../sum'); | ||
|
||
const mockSub = sub as jest.MockedFunction<typeof sub>; | ||
const mockSum = sum as jest.MockedFunction<typeof sum>; | ||
const MockMemory = Memory as jest.MockedClass<typeof Memory>; | ||
|
||
describe('calc - mocks', () => { | ||
const memory = new MockMemory(); | ||
|
||
it('returns result from subtract', () => { | ||
mockSub.mockReturnValueOnce(0); | ||
|
||
const calc = makeCalc(memory); | ||
const result = calc('Sub', [2, 2]); | ||
|
||
expect(result).toEqual(0); | ||
expect(mockSub).toBeCalledWith(2, 2); | ||
}); | ||
|
||
it('returns result from sum', () => { | ||
mockSum.mockReturnValueOnce(2); | ||
|
||
const calc = makeCalc(memory); | ||
const result = calc('Sum', [1, 1]); | ||
|
||
expect(result).toEqual(2); | ||
expect(mockSum).toBeCalledWith(1, 1); | ||
}); | ||
|
||
it('adds last result to memory', () => { | ||
MockMemory.prototype.add.mockImplementationOnce(x => x); | ||
mockSum.mockReturnValueOnce(2); | ||
|
||
const calc = makeCalc(memory); | ||
const sumResult = calc('Sum', [1, 1]); | ||
const memoryResult = calc('MemoryAdd', []); | ||
|
||
expect(sumResult).toEqual(2); | ||
expect(memoryResult).toEqual(2); | ||
expect(MockMemory.prototype.add).toBeCalledWith(2); | ||
}); | ||
|
||
it('subtracts last result to memory', () => { | ||
MockMemory.prototype.subtract.mockImplementationOnce(x => x); | ||
mockSum.mockReturnValueOnce(2); | ||
|
||
const calc = makeCalc(memory); | ||
const sumResult = calc('Sum', [1, 1]); | ||
const memoryResult = calc('MemorySub', []); | ||
|
||
expect(sumResult).toEqual(2); | ||
expect(memoryResult).toEqual(2); | ||
expect(MockMemory.prototype.subtract).toBeCalledWith(2); | ||
}); | ||
|
||
it('clears the memory', () => { | ||
MockMemory.prototype.add.mockImplementationOnce(x => x); | ||
mockSum.mockReturnValueOnce(2).mockReturnValueOnce(4); | ||
|
||
const calc = makeCalc(memory); | ||
const sumResult = calc('Sum', [1, 1]); | ||
const memoryResult = calc('MemoryAdd', []); | ||
const sumResult2 = calc('Sum', [2, 2]); | ||
const clearResult = calc('MemoryClear', []); | ||
|
||
expect(sumResult).toEqual(2); | ||
expect(memoryResult).toEqual(2); | ||
expect(sumResult2).toEqual(4); | ||
expect(clearResult).toEqual(4); | ||
expect(MockMemory.prototype.reset).toBeCalledTimes(1); | ||
}); | ||
|
||
it('throws an error when invalid Op is passed', () => { | ||
const calc = makeCalc(memory); | ||
|
||
// @ts-expect-error | ||
expect(() => calc('Multiply', [2, 3])).toThrowError( | ||
new Error('Invalid op'), | ||
); | ||
}); | ||
}); |
This file contains hidden or 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,6 +1,7 @@ | ||
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
|
||
import sub from '../sub'; | ||
|
||
it('subtracts 5 - 1 to equal 4 in TypeScript', () => { | ||
const sub = require('../sub').default; | ||
expect(sub(5, 1)).toBe(4); | ||
}); |
This file contains hidden or 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 hidden or 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,39 @@ | ||
import sub from './sub'; | ||
import sum from './sum'; | ||
import Memory from './memory'; | ||
|
||
type Op = 'MemoryAdd' | 'MemoryClear' | 'MemorySub' | 'Sub' | 'Sum'; | ||
|
||
export default (memory: Memory) => { | ||
let last = 0; | ||
|
||
return (op: Op, input: Array<number>): number => { | ||
switch (op) { | ||
case 'MemoryAdd': { | ||
return memory.add(last); | ||
} | ||
case 'MemoryClear': { | ||
memory.reset(); | ||
return last; | ||
} | ||
case 'MemorySub': { | ||
return memory.subtract(last); | ||
} | ||
case 'Sub': { | ||
const [a, b] = input; | ||
const result = sub(a, b); | ||
last = result; | ||
return result; | ||
} | ||
case 'Sum': { | ||
const [a, b] = input; | ||
const result = sum(a, b); | ||
last = result; | ||
return result; | ||
} | ||
default: { | ||
throw new Error('Invalid op'); | ||
} | ||
} | ||
}; | ||
}; |
This file contains hidden or 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 @@ | ||
export default class Memory { | ||
current: number; | ||
|
||
constructor() { | ||
this.current = 0; | ||
} | ||
|
||
add(entry: number) { | ||
this.current += entry; | ||
|
||
return this.current; | ||
} | ||
|
||
subtract(entry: number) { | ||
this.current -= entry; | ||
|
||
return this.current; | ||
} | ||
|
||
reset() { | ||
this.current = 0; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.