|
| 1 | +/** |
| 2 | + * Integration tests for main store |
| 3 | + * Tests complete data fetching flow with axios and error handling |
| 4 | + */ |
| 5 | + |
| 6 | +import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest' |
| 7 | +import { setActivePinia, createPinia } from 'pinia' |
| 8 | +import { useMainStore } from './main' |
| 9 | +import axios from 'axios' |
| 10 | +import type { Config } from '@/types' |
| 11 | + |
| 12 | +vi.mock('axios') |
| 13 | + |
| 14 | +describe('Main Store Integration', () => { |
| 15 | + beforeEach(() => { |
| 16 | + setActivePinia(createPinia()) |
| 17 | + vi.clearAllMocks() |
| 18 | + }) |
| 19 | + |
| 20 | + afterEach(() => { |
| 21 | + vi.restoreAllMocks() |
| 22 | + }) |
| 23 | + |
| 24 | + describe('Data Fetching', () => { |
| 25 | + it('fetches books and updates count', async () => { |
| 26 | + const store = useMainStore() |
| 27 | + const mockData = { totalCount: 100, books: [] } |
| 28 | + vi.mocked(axios.get).mockResolvedValue({ data: mockData }) |
| 29 | + |
| 30 | + const result = await store.fetchBooks() |
| 31 | + |
| 32 | + expect(axios.get).toHaveBeenCalledWith('./books.json') |
| 33 | + expect(result).toEqual(mockData) |
| 34 | + expect(store.booksCount).toBe(100) |
| 35 | + expect(store.loading).toBe(false) |
| 36 | + expect(store.errorMessage).toBeNull() |
| 37 | + }) |
| 38 | + |
| 39 | + it('fetches authors and updates count', async () => { |
| 40 | + const store = useMainStore() |
| 41 | + const mockData = { totalCount: 50, authors: [] } |
| 42 | + vi.mocked(axios.get).mockResolvedValue({ data: mockData }) |
| 43 | + |
| 44 | + const result = await store.fetchAuthors() |
| 45 | + |
| 46 | + expect(axios.get).toHaveBeenCalledWith('./authors.json') |
| 47 | + expect(result).toEqual(mockData) |
| 48 | + expect(store.authorsCount).toBe(50) |
| 49 | + expect(store.loading).toBe(false) |
| 50 | + expect(store.errorMessage).toBeNull() |
| 51 | + }) |
| 52 | + |
| 53 | + it('fetches shelves and updates count', async () => { |
| 54 | + const store = useMainStore() |
| 55 | + const mockData = { totalCount: 20, shelves: [] } |
| 56 | + vi.mocked(axios.get).mockResolvedValue({ data: mockData }) |
| 57 | + |
| 58 | + const result = await store.fetchLCCShelves() |
| 59 | + |
| 60 | + expect(axios.get).toHaveBeenCalledWith('./lcc_shelves.json') |
| 61 | + expect(result).toEqual(mockData) |
| 62 | + expect(store.shelvesCount).toBe(20) |
| 63 | + expect(store.loading).toBe(false) |
| 64 | + expect(store.errorMessage).toBeNull() |
| 65 | + }) |
| 66 | + |
| 67 | + it.each([ |
| 68 | + { method: 'fetchBook' as const, param: 42, url: './books/42.json' }, |
| 69 | + { method: 'fetchAuthor' as const, param: 'austen-jane', url: './authors/austen-jane.json' }, |
| 70 | + { method: 'fetchLCCShelf' as const, param: 'PR', url: './lcc_shelves/PR.json' } |
| 71 | + ])('$method fetches single item by ID', async ({ method, param, url }) => { |
| 72 | + const store = useMainStore() |
| 73 | + const mockData = { id: param } |
| 74 | + vi.mocked(axios.get).mockResolvedValue({ data: mockData }) |
| 75 | + |
| 76 | + const result = await store[method](param as never) |
| 77 | + |
| 78 | + expect(axios.get).toHaveBeenCalledWith(url) |
| 79 | + expect(result).toEqual(mockData) |
| 80 | + }) |
| 81 | + |
| 82 | + it('fetches config data', async () => { |
| 83 | + const store = useMainStore() |
| 84 | + const mockConfig: Config = { |
| 85 | + title: 'Gutenberg Library', |
| 86 | + description: 'Project Gutenberg books', |
| 87 | + primaryColor: null, |
| 88 | + secondaryColor: null |
| 89 | + } |
| 90 | + |
| 91 | + vi.mocked(axios.get).mockResolvedValue({ data: mockConfig }) |
| 92 | + |
| 93 | + const result = await store.fetchConfig() |
| 94 | + |
| 95 | + expect(axios.get).toHaveBeenCalledWith('./config.json') |
| 96 | + expect(result).toEqual(mockConfig) |
| 97 | + }) |
| 98 | + }) |
| 99 | + |
| 100 | + describe('Loading State', () => { |
| 101 | + it('sets loading during fetch', async () => { |
| 102 | + const store = useMainStore() |
| 103 | + vi.mocked(axios.get).mockImplementation( |
| 104 | + () => |
| 105 | + new Promise((resolve) => |
| 106 | + setTimeout(() => resolve({ data: { totalCount: 0, books: [] } }), 50) |
| 107 | + ) |
| 108 | + ) |
| 109 | + |
| 110 | + const promise = store.fetchBooks() |
| 111 | + expect(store.loading).toBe(true) |
| 112 | + |
| 113 | + await promise |
| 114 | + expect(store.loading).toBe(false) |
| 115 | + }) |
| 116 | + }) |
| 117 | + |
| 118 | + describe('Error Handling', () => { |
| 119 | + it('sets error message on fetch failure', async () => { |
| 120 | + const store = useMainStore() |
| 121 | + vi.mocked(axios.get).mockRejectedValue(new Error('Network error')) |
| 122 | + |
| 123 | + await expect(store.fetchBooks()).rejects.toThrow('Network error') |
| 124 | + |
| 125 | + expect(store.errorMessage).toBe('Network error') |
| 126 | + expect(store.loading).toBe(false) |
| 127 | + }) |
| 128 | + |
| 129 | + it('uses custom error message when error message is empty', async () => { |
| 130 | + const store = useMainStore() |
| 131 | + vi.mocked(axios.get).mockRejectedValue(new Error('')) |
| 132 | + |
| 133 | + await expect(store.fetchBooks()).rejects.toThrow() |
| 134 | + |
| 135 | + expect(store.errorMessage).toBe('Failed to load books') |
| 136 | + }) |
| 137 | + |
| 138 | + it('clears error message', () => { |
| 139 | + const store = useMainStore() |
| 140 | + store.errorMessage = 'Test error' |
| 141 | + |
| 142 | + store.clearError() |
| 143 | + |
| 144 | + expect(store.errorMessage).toBeNull() |
| 145 | + }) |
| 146 | + |
| 147 | + it('clears previous error on successful fetch', async () => { |
| 148 | + const store = useMainStore() |
| 149 | + store.errorMessage = 'Previous error' |
| 150 | + |
| 151 | + vi.mocked(axios.get).mockResolvedValue({ data: { totalCount: 0, books: [] } }) |
| 152 | + |
| 153 | + await store.fetchBooks() |
| 154 | + |
| 155 | + expect(store.errorMessage).toBeNull() |
| 156 | + }) |
| 157 | + |
| 158 | + it('throws error when response data is empty', async () => { |
| 159 | + const store = useMainStore() |
| 160 | + vi.mocked(axios.get).mockResolvedValue({ data: null }) |
| 161 | + |
| 162 | + await expect(store.fetchBooks()).rejects.toThrow('Empty response from ./books.json') |
| 163 | + expect(store.errorMessage).toContain('Empty response') |
| 164 | + }) |
| 165 | + }) |
| 166 | + |
| 167 | + describe('Request Deduplication', () => { |
| 168 | + it('deduplicates identical concurrent requests', async () => { |
| 169 | + const store = useMainStore() |
| 170 | + const mockData = { totalCount: 0, books: [] } |
| 171 | + vi.mocked(axios.get).mockResolvedValue({ data: mockData }) |
| 172 | + |
| 173 | + const [result1, result2, result3] = await Promise.all([ |
| 174 | + store.fetchBooks(), |
| 175 | + store.fetchBooks(), |
| 176 | + store.fetchBooks() |
| 177 | + ]) |
| 178 | + |
| 179 | + expect(axios.get).toHaveBeenCalledTimes(1) |
| 180 | + expect(result1).toEqual(mockData) |
| 181 | + expect(result2).toEqual(mockData) |
| 182 | + expect(result3).toEqual(mockData) |
| 183 | + }) |
| 184 | + |
| 185 | + it('allows different requests concurrently', async () => { |
| 186 | + const store = useMainStore() |
| 187 | + vi.mocked(axios.get).mockImplementation((url) => { |
| 188 | + if (url === './books.json') { |
| 189 | + return Promise.resolve({ data: { totalCount: 0, books: [] } }) |
| 190 | + } |
| 191 | + return Promise.resolve({ data: { totalCount: 0, authors: [] } }) |
| 192 | + }) |
| 193 | + |
| 194 | + await Promise.all([store.fetchBooks(), store.fetchAuthors()]) |
| 195 | + |
| 196 | + expect(axios.get).toHaveBeenCalledTimes(2) |
| 197 | + }) |
| 198 | + }) |
| 199 | +}) |
0 commit comments