Skip to content

Commit 9309a80

Browse files
committed
feat(cursors): fix signal unit test
1 parent 4295175 commit 9309a80

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

packages/qwik/src/core/reactive-primitives/impl/computed-signal-impl.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { assertFalse } from '../../shared/error/assert';
33
import { QError, qError } from '../../shared/error/error';
44
import type { Container } from '../../shared/types';
55
import { isPromise, maybeThen, retryOnPromise } from '../../shared/utils/promises';
6-
import { tryGetInvokeContext } from '../../use/use-core';
6+
import { invoke, newInvokeContext, tryGetInvokeContext } from '../../use/use-core';
77
import { scheduleEffects, throwIfQRLNotResolved } from '../utils';
88
import { getSubscriber } from '../subscriber';
99
import { SerializationSignalFlags, ComputeQRL, EffectSubscription } from '../types';
@@ -52,8 +52,10 @@ export class ComputedSignalImpl<T, S extends QRLInternal = ComputeQRL<T>>
5252

5353
invalidate() {
5454
this.$flags$ |= SignalFlags.INVALID;
55+
const ctx = newInvokeContext();
56+
ctx.$container$ = this.$container$ || undefined;
5557
maybeThen(
56-
retryOnPromise(() => this.$computeIfNeeded$()),
58+
retryOnPromise(() => invoke.call(this, ctx, this.$computeIfNeeded$)),
5759
() => {
5860
if (this.$flags$ & SignalFlags.RUN_EFFECTS) {
5961
this.$flags$ &= ~SignalFlags.RUN_EFFECTS;

packages/qwik/src/core/reactive-primitives/impl/signal.unit.tsx

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { $, _wrapProp, isBrowser } from '@qwik.dev/core';
22
import { createDocument } from '@qwik.dev/core/testing';
3-
import { afterEach, beforeEach, describe, expect, expectTypeOf, it } from 'vitest';
3+
import { afterEach, beforeEach, describe, expect, expectTypeOf, it, vi } from 'vitest';
44
import { getDomContainer } from '../../client/dom-container';
55
import { implicit$FirstArg } from '../../shared/qrl/implicit_dollar';
66
import { inlinedQrl } from '../../shared/qrl/qrl';
@@ -28,6 +28,7 @@ import {
2828
import { getSubscriber } from '../subscriber';
2929
import { vnode_newVirtual, vnode_setProp } from '../../client/vnode-utils';
3030
import { ELEMENT_SEQ } from '../../shared/utils/markers';
31+
import type { ComputedSignalImpl } from './computed-signal-impl';
3132

3233
class Foo {
3334
constructor(public val: number = 0) {}
@@ -111,10 +112,12 @@ describe('signal', () => {
111112
const log: any[] = [];
112113
const delayMap = new Map();
113114
let container: Container = null!;
115+
let task: Task | null = null;
114116
beforeEach(() => {
115117
log.length = 0;
116118
const document = createDocument({ html: '<html><body q:container="paused"></body></html>' });
117119
container = getDomContainer(document.body);
120+
task = null;
118121
});
119122

120123
afterEach(async () => {
@@ -189,25 +192,39 @@ describe('signal', () => {
189192
await withContainer(async () => {
190193
const a = createSignal(true) as InternalSignal<boolean>;
191194
const b = createSignal(true) as InternalSignal<boolean>;
192-
let signal!: InternalReadonlySignal<boolean>;
195+
let signal!: ComputedSignalImpl<boolean>;
196+
197+
(globalThis as any).waitPromiseResolve = null;
198+
const waitPromise = new Promise<void>((resolve) => {
199+
(globalThis as any).waitPromiseResolve = resolve;
200+
});
201+
193202
await retryOnPromise(() =>
194203
effect$(async () => {
195204
signal =
196205
signal ||
197206
createComputedQrl(
198207
delayQrl(
199208
$(() => {
200-
return a.value || b.value;
209+
const val = a.value || b.value;
210+
if (!val) {
211+
// resolve promise after next macro task
212+
setTimeout(() => {
213+
(globalThis as any).waitPromiseResolve!();
214+
});
215+
}
216+
return val;
201217
})
202218
)
203219
);
204-
const signalValue = await retryOnPromise(() => signal.value);
205-
log.push(signalValue); // causes subscription
220+
log.push(signal.value); // causes subscription
206221
})
207222
);
208223
expect(log).toEqual([true]);
209224
a.value = !a.untrackedValue;
210225
b.value = !b.untrackedValue;
226+
227+
await waitPromise;
211228
expect(log).toEqual([true, false]);
212229
});
213230
});
@@ -286,7 +303,7 @@ describe('signal', () => {
286303
function effectQrl(fnQrl: QRL<() => void>) {
287304
const qrl = fnQrl as QRLInternal<() => void>;
288305
const element: HostElement = vnode_newVirtual();
289-
const task = new Task(0, 0, element, fnQrl as QRLInternal, undefined, null);
306+
task = task || new Task(0, 0, element, fnQrl as QRLInternal, undefined, null);
290307
vnode_setProp(element, ELEMENT_SEQ, [task]);
291308
if (!qrl.resolved) {
292309
throw qrl.resolve();

0 commit comments

Comments
 (0)