Created
September 8, 2020 15:57
-
-
Save bmansk8/d63539a94000e3ad88c36e86ce5fb466 to your computer and use it in GitHub Desktop.
Learning to use jest!
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
[ | |
{ | |
"id": 1, | |
"name": "Chicago Pizza", | |
"image": "/images/chicago-pizza.jpg", | |
"desc": "The pan in which it is baked gives the pizza its characteristically high edge which provides ample space for large amounts of cheese and a chunky tomato sauce.", | |
"price": 9 | |
}, | |
{ | |
"id": 2, | |
"name": "Neapolitan Pizza", | |
"image": "/images/neapolitan-pizza.jpg", | |
"desc": "This style of pizza is prepared with simple and fresh ingredients: a basic dough, raw tomatoes, fresh mozzarella cheese, fresh basil, and olive oil. A fantastic original pizza.", | |
"price": 7 | |
}, | |
{ | |
"id": 3, | |
"name": "New York Pizza", | |
"image": "/images/ny-pizza.jpg", | |
"desc": "New York-style pizza has slices that are large and wide with a thin crust that is foldable yet crispy. It is traditionally topped with tomato sauce and mozzarella cheese.", | |
"price": 8 | |
}, | |
{ | |
"id": 4, | |
"name": "Sicilian Pizza", | |
"image": "/images/sicilian-pizza.jpg", | |
"desc": "Sicilian pizza is pizza prepared in a manner that originated in Sicily, Italy. Sicilian pizza is also known as sfincione or focaccia with toppings. A great tasteful pizza all around.", | |
"price": 9 | |
} | |
] |
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
import pizzas from '../data.json'; | |
test('pizza data is correct ', () => { | |
expect(pizzas).toMatchSnapshot(); | |
expect(pizzas).toHaveLength(4); | |
expect(pizzas.map(pizza => pizza.name)).toEqual([ | |
'Chicago Pizza', | |
'Neapolitan Pizza', | |
'New York Pizza', | |
'Sicilian Pizza' | |
]) | |
}) | |
for (let i = 0; i < pizzas.length; i++) { | |
test(`pizza[${i}] should have (id, name, image, desc, price)`, () => { | |
expect(pizzas[i]).toHaveProperty('id'); | |
expect(pizzas[i]).toHaveProperty('name'); | |
expect(pizzas[i]).toHaveProperty('image'); | |
expect(pizzas[i]).toHaveProperty('desc'); | |
expect(pizzas[i]).toHaveProperty('price'); | |
}); | |
} | |
test('mock imp of basic funtion ', () => { | |
const mock = jest.fn(() => 'Im a mock function'); | |
expect(mock('calling my mock function')).toBe('Im a mock function') | |
expect(mock).toHaveBeenCalledWith('calling my mock function') | |
}) | |
test('mock return value of func one time', () => { | |
const mock =jest.fn(); | |
mock.mockReturnValueOnce('Hello') | |
.mockReturnValueOnce('There') | |
mock(); | |
mock(); | |
expect(mock).toHaveBeenCalledTimes(2); | |
mock('Hello', 'There', 'Steve'); | |
expect(mock).toHaveBeenCalledWith('Hello','There','Steve') | |
mock('Steve'); | |
expect(mock).toHaveBeenLastCalledWith('Steve'); | |
}) | |
test('mock imp of function ', () => { | |
const mock = jest.fn().mockImplementation(() => 'United Kingdom'); | |
expect(mock('Location')).toBe('United Kingdom'); | |
expect(mock).toHaveBeenCalledWith('Location'); | |
}) | |
test('spying using og imp', () => { | |
const pizza={ | |
name: n =>`Pizza name: ${n}` | |
}; | |
const spy = jest.spyOn(pizza, 'name'); | |
expect(pizza.name('Cheese')).toBe('Pizza name: Cheese') | |
expect(spy).toHaveBeenCalledWith('Cheese'); | |
}) | |
test('spying using mock imp', () => { | |
const pizza={ | |
name: n =>`Pizza name: ${n}` | |
}; | |
const spy = jest.spyOn(pizza, 'name'); | |
spy.mockImplementation(n => `crazy pizza`) | |
expect(pizza.name('Cheese')).toBe('crazy pizza') | |
spy.mockRestore(); | |
expect(pizza.name('Cheese')).toBe('Pizza name: Cheese'); | |
}) | |
test('pizza returns new york last', () => { | |
const pizza0 = pizzas[0] | |
const pizza1 = pizzas[1] | |
const pizza2 = pizzas[2] | |
const pizza = jest.fn(currentPizza => currentPizza.name); | |
pizza(pizza0); | |
pizza(pizza1); | |
pizza(pizza2); | |
expect(pizza).toHaveLastReturnedWith('New York Pizza') | |
}) | |
test('pizza data has new york pizza and is a object', () => { | |
const newYorkPizza ={ | |
"id": 3, | |
"name": "New York Pizza", | |
"image": "/images/ny-pizza.jpg", | |
"desc": "New York-style pizza has slices that are large and wide with a thin crust that is foldable yet crispy. It is traditionally topped with tomato sauce and mozzarella cheese.", | |
"price": 8 | |
} | |
expect(pizzas[2]).toMatchObject(newYorkPizza); | |
}) | |
test('expect promise to resolve', async () => { | |
const user ={ | |
getFullName: jest.fn(()=> Promise.resolve('Karl Hadwen')) | |
}; | |
await expect(user.getFullName('Karl Hadwen')).resolves.toBe('Karl Hadwen') | |
}) | |
test('expect promise to reject', async () => { | |
const user ={ | |
getFullName: jest.fn(()=> Promise.reject(new Error('something is terribly wrong'))) | |
}; | |
await expect(user.getFullName('Karl Hadwen')).rejects.toThrow('something is terribly wrong') | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment