blob: d0a05bfdf84887aa71c044574ecc82dd1ba945af (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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');
});
});
|