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
213import { test, expect } from '@playwright/test';
test.describe('observables-react', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/');
});
test.describe('obsView - Basic Counter', () => {
test('renders with initial value', async ({ page }) => {
const countValue = page.getByTestId('count-value');
await expect(countValue).toHaveText('10');
});
test('increments count on button click', async ({ page }) => {
const countValue = page.getByTestId('count-value');
const incrementBtn = page.getByTestId('increment-btn');
await expect(countValue).toHaveText('10');
await incrementBtn.click();
await expect(countValue).toHaveText('11');
await incrementBtn.click();
await expect(countValue).toHaveText('12');
});
test('decrements count on button click', async ({ page }) => {
const countValue = page.getByTestId('count-value');
const decrementBtn = page.getByTestId('decrement-btn');
await expect(countValue).toHaveText('10');
await decrementBtn.click();
await expect(countValue).toHaveText('9');
});
});
test.describe('ObsView - Inline Rendering', () => {
test('renders observable value inline', async ({ page }) => {
const obsViewValue = page.getByTestId('obsview-value');
await expect(obsViewValue).toHaveText('0');
});
test('updates when observable changes', async ({ page }) => {
const obsViewValue = page.getByTestId('obsview-value');
const incrementBtn = page.getByTestId('obsview-increment');
await incrementBtn.click();
await expect(obsViewValue).toHaveText('1');
await incrementBtn.click();
await expect(obsViewValue).toHaveText('2');
});
});
test.describe('Derived Values', () => {
test('computes derived values correctly', async ({ page }) => {
const baseValue = page.getByTestId('base-value');
const doubledValue = page.getByTestId('doubled-value');
const tripledValue = page.getByTestId('tripled-value');
await expect(baseValue).toHaveText('5');
await expect(doubledValue).toHaveText('10');
await expect(tripledValue).toHaveText('15');
});
test('updates derived values when base changes', async ({ page }) => {
const baseValue = page.getByTestId('base-value');
const doubledValue = page.getByTestId('doubled-value');
const tripledValue = page.getByTestId('tripled-value');
const incrementBtn = page.getByTestId('base-increment');
await incrementBtn.click();
await expect(baseValue).toHaveText('6');
await expect(doubledValue).toHaveText('12');
await expect(tripledValue).toHaveText('18');
});
});
test.describe('view() with Props', () => {
test('renders props correctly', async ({ page }) => {
const labelElement = page.getByTestId('view-label');
const countElement = page.getByTestId('view-count');
await expect(labelElement).toHaveText('Hello');
await expect(countElement).toHaveText('42');
});
test('updates when props change', async ({ page }) => {
const labelElement = page.getByTestId('view-label');
const countElement = page.getByTestId('view-count');
const updateBtn = page.getByTestId('update-view-props');
await updateBtn.click();
await expect(labelElement).toHaveText('Updated');
await expect(countElement).toHaveText('100');
});
});
test.describe('viewWithModel', () => {
test('renders model state', async ({ page }) => {
const modelCount = page.getByTestId('model-count');
const modelIsEven = page.getByTestId('model-is-even');
await expect(modelCount).toHaveText('0');
await expect(modelIsEven).toHaveText('even');
});
test('updates model state on increment', async ({ page }) => {
const modelCount = page.getByTestId('model-count');
const modelIsEven = page.getByTestId('model-is-even');
const incrementBtn = page.getByTestId('model-increment');
await incrementBtn.click();
await expect(modelCount).toHaveText('1');
await expect(modelIsEven).toHaveText('odd');
});
test('derived values update correctly', async ({ page }) => {
const modelCount = page.getByTestId('model-count');
const modelIsEven = page.getByTestId('model-is-even');
const incrementBtn = page.getByTestId('model-increment');
await incrementBtn.click();
await incrementBtn.click();
await expect(modelCount).toHaveText('2');
await expect(modelIsEven).toHaveText('even');
});
});
test.describe('val() Helper', () => {
test('renders observable value', async ({ page }) => {
const valOutput = page.getByTestId('val-output');
await expect(valOutput).toHaveText('initial');
});
test('updates when observable changes', async ({ page }) => {
const valOutput = page.getByTestId('val-output');
const updateBtn = page.getByTestId('val-update');
await updateBtn.click();
await expect(valOutput).toHaveText('updated');
});
});
test.describe('prop.const and prop.obs', () => {
test('renders constant and observable props', async ({ page }) => {
const constValue = page.getByTestId('const-value');
const obsValue = page.getByTestId('obs-value');
await expect(constValue).toHaveText('constant');
await expect(obsValue).toHaveText('observable-value');
});
test('updates observable prop when changed', async ({ page }) => {
const obsValue = page.getByTestId('obs-value');
const updateBtn = page.getByTestId('update-obs-prop');
await updateBtn.click();
await expect(obsValue).toHaveText('updated-observable');
});
});
test.describe('Render Efficiency', () => {
test('re-renders when observable changes', async ({ page }) => {
const trackerValue = page.getByTestId('tracker-value');
const renderCount = page.getByTestId('render-count');
const updateBtn = page.getByTestId('tracker-update');
const initialRenderCount = await renderCount.textContent();
await updateBtn.click();
await expect(trackerValue).toHaveText('1');
const newRenderCount = await renderCount.textContent();
expect(Number(newRenderCount)).toBeGreaterThan(Number(initialRenderCount));
});
});
test.describe('Dependency Injection', () => {
test('injects services into ViewModel', async ({ page }) => {
const greeting = page.getByTestId('di-greeting').first();
await expect(greeting).toHaveText('Hello, World!');
});
test('injected service responds to observable changes', async ({ page }) => {
const greeting = page.getByTestId('di-greeting').first();
const nameInput = page.getByTestId('di-name-input').first();
await nameInput.fill('Playwright');
await expect(greeting).toHaveText('Hello, Playwright!');
});
test('multiple services can be injected', async ({ page }) => {
const counter = page.getByTestId('di-counter').first();
const incrementBtn = page.getByTestId('di-increment').first();
await expect(counter).toHaveText('0');
await incrementBtn.click();
await expect(counter).toHaveText('1');
await incrementBtn.click();
await expect(counter).toHaveText('2');
});
test('child container overrides parent service', async ({ page }) => {
// Child container overrides GreetingService with "Hi there" instead of "Hello"
const childGreeting = page.getByTestId('di-greeting').nth(1);
await expect(childGreeting).toHaveText('Hi there, World!');
});
});
});