diff options
| author | Wes Bos <wesbos@gmail.com> | 2018-09-07 12:15:53 -0400 |
|---|---|---|
| committer | Wes Bos <wesbos@gmail.com> | 2018-09-07 12:15:53 -0400 |
| commit | 093c172d16732ba8d01faa313e0af99d26902205 (patch) | |
| tree | 84838c4f6410b43f42b56219190a62e14e072ef3 /stepped-solutions/57/frontend/__tests__/mocking.test.js | |
| parent | 9876e7dfca6fa2bbf6ac383eeb8e2c82e05c2164 (diff) | |
getting there
Diffstat (limited to 'stepped-solutions/57/frontend/__tests__/mocking.test.js')
| -rwxr-xr-x | stepped-solutions/57/frontend/__tests__/mocking.test.js | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/stepped-solutions/57/frontend/__tests__/mocking.test.js b/stepped-solutions/57/frontend/__tests__/mocking.test.js new file mode 100755 index 0000000..d0a05bf --- /dev/null +++ b/stepped-solutions/57/frontend/__tests__/mocking.test.js @@ -0,0 +1,36 @@ +function Person(name, foods) { + this.name = name; + this.foods = foods; +} + +Person.prototype.fetchFavFoods = function() { + return new Promise((resolve, reject) => { + // Simulate an API + setTimeout(() => resolve(this.foods), 2000); + }); +}; + +describe('mocking learning', () => { + it('mocks a reg function', () => { + const fetchDogs = jest.fn(); + fetchDogs('snickers'); + expect(fetchDogs).toHaveBeenCalled(); + expect(fetchDogs).toHaveBeenCalledWith('snickers'); + fetchDogs('hugo'); + expect(fetchDogs).toHaveBeenCalledTimes(2); + }); + + it('can create a person', () => { + const me = new Person('Wes', ['pizza', 'burgs']); + expect(me.name).toBe('Wes'); + }); + + it('can fetch foods', async () => { + const me = new Person('Wes', ['pizza', 'burgs']); + // mock the favFoods function + me.fetchFavFoods = jest.fn().mockResolvedValue(['sushi', 'ramen']); + const favFoods = await me.fetchFavFoods(); + console.log(favFoods); + expect(favFoods).toContain('sushi'); + }); +}); |
