@softwarity/livewire 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,183 +0,0 @@
1
- import { ChangeDetectorRef, Component } from '@angular/core';
2
- import { TestBed } from '@angular/core/testing';
3
- import { Subject } from 'rxjs';
4
- import { LiveWindowDataSource } from '../src/lib/live-window.datasource';
5
- import type { LiveRow, UpdateFrame } from '@softwarity/livewire-protocol';
6
-
7
- /**
8
- * What repaints a zoneless screen when a frame lands in a socket callback.
9
- *
10
- * The data source injects the view's `ChangeDetectorRef` and calls
11
- * `markForCheck()`, which marks the view dirty *and* notifies the zoneless
12
- * scheduler. Nothing is asked of the screen.
13
- *
14
- * The component below reads **no signal at all** and uses no async pipe - it
15
- * renders a plain field, filled from a subscription. That is deliberate: it
16
- * strips out every other thing that could schedule a pass, so what is measured
17
- * is the notification itself and nothing else.
18
- */
19
- describe('repainting a zoneless screen', () => {
20
- const feed = new Subject<UpdateFrame<LiveRow>>();
21
-
22
- function frame(...ids: string[]): UpdateFrame<LiveRow> {
23
- return {
24
- id: 'w',
25
- type: 'snapshot',
26
- rows: ids.map((id) => ({ id, updatedAt: 'v1' })),
27
- total: ids.length,
28
- sequence: 1,
29
- };
30
- }
31
-
32
- @Component({
33
- selector: 'test-screen',
34
- standalone: true,
35
- template: `<span class="ids">{{ ids }}</span>`,
36
- })
37
- class ScreenComponent {
38
- ids = '';
39
-
40
- // A component field, which is an injection context - the data source takes
41
- // this view's ChangeDetectorRef from it.
42
- readonly source = new LiveWindowDataSource<LiveRow>();
43
-
44
- constructor() {
45
- this.source.changes.subscribe((rows) => (this.ids = rows.map((row) => row?.id ?? '-').join(',')));
46
- this.source.reset(() => feed);
47
- }
48
- }
49
-
50
- it('repaints on a frame arriving from outside a change-detection cycle', async () => {
51
- const fixture = TestBed.createComponent(ScreenComponent);
52
- await fixture.whenStable();
53
-
54
- // Straight into the data source, as a socket callback would.
55
- feed.next(frame('a', 'b'));
56
- await fixture.whenStable();
57
-
58
- expect(fixture.nativeElement.querySelector('.ids').textContent).toBe('a,b');
59
- });
60
-
61
- it('repaints again on every later frame, not only the first', async () => {
62
- const fixture = TestBed.createComponent(ScreenComponent);
63
- await fixture.whenStable();
64
-
65
- for (const ids of [['a'], ['a', 'b'], ['a', 'b', 'c']]) {
66
- feed.next(frame(...ids));
67
- await fixture.whenStable();
68
- expect(fixture.nativeElement.querySelector('.ids').textContent).toBe(ids.join(','));
69
- }
70
- });
71
-
72
- /**
73
- * The rule made structural: there is no way to build one that cannot repaint.
74
- * Left optional, the screens that forgot it would look right in development
75
- * and hold stale rows in production.
76
- */
77
- it('refuses to be built where there is no view to repaint', () => {
78
- expect(() => new LiveWindowDataSource<LiveRow>()).toThrow();
79
- });
80
- });
81
-
82
- /**
83
- * The other half of "it repaints": that it does so only when there is something
84
- * to repaint.
85
- *
86
- * A pass asked for on every read rather than on every change is how a live
87
- * screen becomes one nobody can use - and it would be invisible, since the
88
- * screen would look right the whole time.
89
- */
90
- describe('what asks for a repaint, and what does not', () => {
91
- let asked: number;
92
- let feed: Subject<UpdateFrame<LiveRow>>;
93
- let source: LiveWindowDataSource<LiveRow>;
94
-
95
- function rows(count: number): LiveRow[] {
96
- return Array.from({ length: count }, (_, index) => ({ id: `r${index}`, updatedAt: 'v1' }));
97
- }
98
-
99
- beforeEach(() => {
100
- jest.useFakeTimers();
101
- asked = 0;
102
- // `ChangeDetectorRef` is a token like any other: provided here, the data
103
- // source injects this instead of a view's, and the calls can be counted.
104
- // Only `markForCheck` is ever called.
105
- TestBed.configureTestingModule({
106
- providers: [{ provide: ChangeDetectorRef, useValue: { markForCheck: () => (asked += 1) } }],
107
- });
108
- feed = new Subject<UpdateFrame<LiveRow>>();
109
- source = TestBed.runInInjectionContext(() => new LiveWindowDataSource<LiveRow>());
110
- });
111
-
112
- afterEach(() => jest.useRealTimers());
113
-
114
- it('asks once when the list is emptied for a new question', () => {
115
- source.reset(() => feed);
116
-
117
- expect(asked).toBe(1);
118
- });
119
-
120
- it('asks once per frame it could apply, and no more', () => {
121
- source.reset(() => feed);
122
- asked = 0;
123
-
124
- feed.next({ id: 'w', type: 'snapshot', rows: rows(3), total: 3, sequence: 1 });
125
- expect(asked).toBe(1);
126
-
127
- feed.next({
128
- id: 'w',
129
- type: 'patch',
130
- upserted: [{ id: 'new', updatedAt: 'v1' }],
131
- removed: [],
132
- order: ['new', 'r0', 'r1', 'r2'],
133
- total: 4,
134
- sequence: 2,
135
- });
136
- expect(asked).toBe(2);
137
- });
138
-
139
- /** A frame it cannot place changes nothing on the screen. It resyncs instead. */
140
- it('asks for nothing on a frame it had to reject', () => {
141
- source.reset(() => feed);
142
- feed.next({ id: 'w', type: 'snapshot', rows: rows(3), total: 3, sequence: 1 });
143
- asked = 0;
144
-
145
- // Sequence 3 where 2 was due.
146
- feed.next({ id: 'w', type: 'patch', upserted: [], removed: [], order: ['r0'], total: 1, sequence: 3 });
147
-
148
- expect(asked).toBe(0);
149
- });
150
-
151
- /** Moving the window is a question, not an answer. The snapshot repaints. */
152
- it('asks for nothing when the window moves, only when its answer lands', () => {
153
- source.reset(() => feed);
154
- feed.next({ id: 'w', type: 'snapshot', rows: rows(100), total: 13_000, sequence: 1 });
155
- asked = 0;
156
-
157
- source.ensure(9000);
158
-
159
- expect(asked).toBe(0);
160
- });
161
-
162
- /** The marks have to come off, and taking them off is a change like any other. */
163
- it('asks once more when the fresh marks expire, and then stops', () => {
164
- source.reset(() => feed);
165
- feed.next({ id: 'w', type: 'snapshot', rows: rows(3), total: 3, sequence: 1 });
166
- feed.next({
167
- id: 'w',
168
- type: 'patch',
169
- upserted: [{ id: 'new', updatedAt: 'v1' }],
170
- removed: [],
171
- order: ['new', 'r0', 'r1', 'r2'],
172
- total: 4,
173
- sequence: 2,
174
- });
175
- asked = 0;
176
-
177
- jest.advanceTimersByTime(1000);
178
- expect(asked).toBe(1);
179
-
180
- jest.advanceTimersByTime(10_000);
181
- expect(asked).toBe(1);
182
- });
183
- });
package/test/setup.ts DELETED
@@ -1,6 +0,0 @@
1
- // Zoneless, deliberately: this library is written for applications that do not
2
- // ship zone.js, and its one contract with change detection - `markForCheck()`
3
- // in the template - only means anything there.
4
- import { setupZonelessTestEnv } from 'jest-preset-angular/setup-env/zoneless';
5
-
6
- setupZonelessTestEnv();
package/tsconfig.json DELETED
@@ -1,18 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2022",
4
- "module": "ES2022",
5
- "moduleResolution": "bundler",
6
- "lib": ["ES2022", "DOM"],
7
- "declaration": true,
8
- "experimentalDecorators": true,
9
- "importHelpers": true,
10
- "strict": true,
11
- "noImplicitOverride": true,
12
- "skipLibCheck": true,
13
- "esModuleInterop": true,
14
- "forceConsistentCasingInFileNames": true
15
- },
16
- "angularCompilerOptions": { "strictTemplates": true },
17
- "include": ["src/**/*.ts"]
18
- }
@@ -1,5 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": { "types": ["jest", "node"] },
4
- "include": ["src/**/*.ts", "test/**/*.ts"]
5
- }