-
-
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.
Update TypeScript example to show use of newer Jest types (#10399)
- Loading branch information
1 parent
63409db
commit 1b8f70a
Showing
6 changed files
with
155 additions
and
1 deletion.
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,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 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 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,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 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; | ||
} | ||
} |