-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDao.test.tsx
69 lines (59 loc) · 2.29 KB
/
Dao.test.tsx
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import 'react-native';
import { Dao } from '../src/business/dao/Dao';
import { inject, storage } from '../src/inject';
import { Portfolio } from '../src/business/state/Portfolio';
import { Stock } from '../src/business/state/Stock';
import { CashBalance } from '../src/business/state/CashBalance';
import { SimulatedDao } from '../src/business/dao/SimulatedDao';
describe('Dao', () => {
let dao: Dao;
let mockStorage: jest.MockedFunction<any>;
beforeEach(() => {
inject({});
mockStorage = {
setItem: jest.spyOn(storage, 'setItem'),
getItem: jest.spyOn(storage, 'getItem')
};
dao = new SimulatedDao();
});
afterEach(() => {
jest.restoreAllMocks();
});
it('saves portfolio.', async () => {
const portfolio = new Portfolio({
stocks: [new Stock('AAPL', 10, 150)],
cashBalance: new CashBalance(1000)
})
;
await dao.savePortfolio(portfolio);
let result = await storage.getItem('portfolio');
expect(result).toBe(JSON.stringify(portfolio));
expect(mockStorage.setItem).toHaveBeenCalledWith('portfolio', JSON.stringify(portfolio));
});
it('loads portfolio when portfolio exists.', async () => {
const portfolio = new Portfolio({
stocks: [new Stock('AAPL', 10, 150)],
cashBalance: new CashBalance(1000)
});
mockStorage.getItem.mockResolvedValue(JSON.stringify(portfolio));
const loadedPortfolio = await dao.loadPortfolio();
expect(loadedPortfolio).toEqual(portfolio);
});
it('loads portfolio when portfolio does not exist.', async () => {
mockStorage.getItem.mockResolvedValue(null);
const loadedPortfolio = await dao.loadPortfolio();
expect(loadedPortfolio).toEqual(new Portfolio());
});
it('throws error when saving portfolio fails', async () => {
const portfolio = new Portfolio({
stocks: [new Stock('AAPL', 10, 150)],
cashBalance: new CashBalance(1000)
});
mockStorage.setItem.mockRejectedValue(new Error('Failed to save portfolio'));
await expect(dao.savePortfolio(portfolio)).rejects.toThrow('Failed to save portfolio');
});
it('throws error when loading portfolio fails', async () => {
mockStorage.getItem.mockRejectedValue(new Error('Failed to load portfolio'));
await expect(dao.loadPortfolio()).rejects.toThrow('Failed to load portfolio');
});
});