Skip to content

Commit 8472532

Browse files
committed
Unit tests of Pages and Components
1 parent f3c97e4 commit 8472532

File tree

4 files changed

+139
-1
lines changed

4 files changed

+139
-1
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@ buck-out/
5959
coverage
6060

6161
# Snapshots
62-
__tests__/components/__snapshots__
62+
__tests__/**/__snapshots__
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from 'react';
2+
import ReactTestRender from 'react-test-renderer';
3+
4+
import PlaceholderLoading from '~/components/PlaceholderLoading';
5+
6+
let wrapper;
7+
8+
beforeEach(() => {
9+
wrapper = ReactTestRender.create(<PlaceholderLoading />);
10+
});
11+
12+
afterEach(() => {
13+
wrapper.unmount();
14+
});
15+
16+
describe('PlaceholderLoading page', () => {
17+
describe('Smoke tests', () => {
18+
it('Should render PlaceholderLoading page correctly', () => {
19+
expect(wrapper.toJSON()).toMatchSnapshot();
20+
});
21+
});
22+
});

__tests__/pages/Cart.spec.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import React from 'react';
2+
import { Provider } from 'react-redux';
3+
import createStore from 'redux-mock-store';
4+
import ReactTestRender from 'react-test-renderer';
5+
6+
import Cart from '~/pages/Cart';
7+
8+
const mockStore = createStore();
9+
10+
const INITIAL_STATE = {
11+
cart: {
12+
items: [
13+
{
14+
id: 1,
15+
name: 'Camiseta Hyperas Preta',
16+
brand: 'Quiksilver',
17+
image: 'https://t-static.dafiti.com.br/cer-243-1-product.jpg',
18+
price: 49.99,
19+
quantity: 2,
20+
},
21+
],
22+
},
23+
};
24+
25+
const EMPTY_STATE = {
26+
cart: {
27+
items: [],
28+
}
29+
}
30+
31+
let wrapper;
32+
let wrapperEmpty;
33+
34+
beforeEach(() => {
35+
wrapper = ReactTestRender.create(
36+
<Provider store={mockStore(INITIAL_STATE)}>
37+
<Cart />
38+
</Provider>,
39+
);
40+
41+
wrapperEmpty = ReactTestRender.create(
42+
<Provider store={mockStore(EMPTY_STATE)}>
43+
<Cart />
44+
</Provider>,
45+
);
46+
});
47+
48+
afterEach(() => {
49+
wrapper.unmount();
50+
wrapperEmpty.unmount();
51+
});
52+
53+
describe('Cart page', () => {
54+
describe('Smoke tests', () => {
55+
it('Should render Cart page correctly', () => {
56+
expect(wrapper.toJSON()).toMatchSnapshot();
57+
});
58+
});
59+
60+
describe('Page structure', () => {
61+
it('Should render empty cart message when items array is empty', () => {
62+
expect(wrapperEmpty.root.findAllByType('Text')[0].props.children).toEqual('There are no products in the cart.');
63+
});
64+
65+
it('Should render products when items prop is not empty - Brand', () => {
66+
expect(wrapper.root.findAllByType('Text')[1].props.children).toEqual('Quiksilver');
67+
});
68+
});
69+
});

__tests__/pages/Product.spec.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import React from 'react';
2+
import { Provider } from 'react-redux';
3+
import createStore from 'redux-mock-store';
4+
import ReactTestRender from 'react-test-renderer';
5+
6+
const mockStore = createStore();
7+
8+
import Product from '~/pages/Product';
9+
10+
const navigationParam = {
11+
id: 1,
12+
name: 'Camiseta Hyperas Preta',
13+
brand: 'Quiksilver',
14+
image: 'https://t-static.dafiti.com.br/cer-243-1-product.jpg',
15+
price: 49.99,
16+
quantity: 2,
17+
};
18+
19+
const navigation = { navigate: jest.fn(), getParam: jest.fn(() => navigationParam) };
20+
21+
let wrapper;
22+
23+
beforeEach(() => {
24+
wrapper = ReactTestRender.create(
25+
<Provider store={mockStore({})}>
26+
<Product navigation={navigation} />
27+
</Provider>,
28+
);
29+
});
30+
31+
afterEach(() => {
32+
wrapper.unmount();
33+
});
34+
35+
describe('Product page', () => {
36+
describe('Smoke tests', () => {
37+
it('Should render Product page correctly', () => {
38+
expect(wrapper.toJSON()).toMatchSnapshot();
39+
});
40+
});
41+
42+
describe('Page structure', () => {
43+
it('Should render product details correctly - Image', () => {
44+
expect(wrapper.root.findAllByType('Image').length).toEqual(1);
45+
});
46+
});
47+
});

0 commit comments

Comments
 (0)