-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-simple.js
More file actions
213 lines (178 loc) Β· 4.93 KB
/
test-simple.js
File metadata and controls
213 lines (178 loc) Β· 4.93 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Simple test script to verify business logic
const { makeObservable, observable, action, runInAction } = require('mobx');
// Mock fetch
global.fetch = jest.fn();
// Mock ApiGateway
class MockApiGateway {
constructor() {
this.get = jest.fn();
this.post = jest.fn();
}
}
// Mock BooksRepository
class MockBooksRepository {
constructor() {
this.httpGateway = new MockApiGateway();
}
getBooks = async () => {
return await this.httpGateway.get("/");
};
addBook = async ({ name, author }) => {
const result = await this.httpGateway.post("/books", { name, author });
return result && result.status === "ok" ? true : false;
};
}
// Test BooksStore
class BooksStore {
books = [];
isLoading = false;
error = null;
booksRepository;
constructor() {
this.booksRepository = new MockBooksRepository();
makeObservable(this, {
books: observable,
isLoading: observable,
error: observable,
loadBooks: action,
addBook: action,
setError: action,
clearError: action
});
}
loadBooks = async () => {
this.isLoading = true;
this.clearError();
try {
const books = await this.booksRepository.getBooks();
runInAction(() => {
this.books = books;
this.isLoading = false;
});
} catch (error) {
runInAction(() => {
this.error = error.message;
this.isLoading = false;
});
}
};
addBook = async (name, author) => {
this.isLoading = true;
this.clearError();
try {
const success = await this.booksRepository.addBook({ name, author });
if (success) {
await this.loadBooks();
} else {
runInAction(() => {
this.error = "Failed to add book";
this.isLoading = false;
});
}
} catch (error) {
runInAction(() => {
this.error = error.message;
this.isLoading = false;
});
}
};
setError = (error) => {
this.error = error;
};
clearError = () => {
this.error = null;
};
get booksCount() {
return this.books.length;
}
}
// Test BooksController
class BooksController {
store;
newBookName = "";
newBookAuthor = "";
constructor(store) {
this.store = store;
makeObservable(this, {
newBookName: observable,
newBookAuthor: observable,
setNewBookName: action,
setNewBookAuthor: action,
handleAddBook: action,
resetForm: action,
isFormValid: computed
});
}
setNewBookName = (name) => {
this.newBookName = name;
};
setNewBookAuthor = (author) => {
this.newBookAuthor = author;
};
handleAddBook = async () => {
if (!this.isFormValid) return;
await this.store.addBook(this.newBookName, this.newBookAuthor);
this.resetForm();
};
resetForm = () => {
this.newBookName = "";
this.newBookAuthor = "";
};
get isFormValid() {
return this.newBookName.trim() !== "" && this.newBookAuthor.trim() !== "";
}
get books() {
return this.store.books;
}
get isLoading() {
return this.store.isLoading;
}
get error() {
return this.store.error;
}
get booksCount() {
return this.store.booksCount;
}
}
// Run tests
console.log('π§ͺ Running simple tests...\n');
// Test 1: BooksStore initialization
console.log('Test 1: BooksStore initialization');
const store = new BooksStore();
console.log('β
Store created successfully');
console.log('β
Initial books count:', store.booksCount);
console.log('β
Initial loading state:', store.isLoading);
console.log('β
Initial error state:', store.error);
// Test 2: Form validation
console.log('\nTest 2: Form validation');
const controller = new BooksController(store);
console.log('β
Controller created successfully');
console.log('β
Empty form validation:', controller.isFormValid);
controller.setNewBookName('Test Book');
controller.setNewBookAuthor('Test Author');
console.log('β
Filled form validation:', controller.isFormValid);
// Test 3: Loading books
console.log('\nTest 3: Loading books');
const mockBooks = [
{ name: 'Book 1', author: 'Author 1' },
{ name: 'Book 2', author: 'Author 2' }
];
store.booksRepository.httpGateway.get.mockResolvedValue(mockBooks);
store.loadBooks().then(() => {
console.log('β
Books loaded successfully');
console.log('β
Books count after loading:', store.booksCount);
console.log('β
Loading state after loading:', store.isLoading);
console.log('β
Books data:', store.books);
});
// Test 4: Adding book
console.log('\nTest 4: Adding book');
store.booksRepository.httpGateway.post.mockResolvedValue({ status: 'ok' });
store.booksRepository.httpGateway.get.mockResolvedValue([
...mockBooks,
{ name: 'New Book', author: 'New Author' }
]);
controller.handleAddBook().then(() => {
console.log('β
Book added successfully');
console.log('β
Form reset after adding:', controller.newBookName === '' && controller.newBookAuthor === '');
});
console.log('\nπ All tests completed!');