Skip to content

Commit 7925db2

Browse files
committed
feat: GC policy tests
1 parent 271e85f commit 7925db2

5 files changed

Lines changed: 161 additions & 2 deletions

File tree

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import { GCPolicy } from '@data-client/core';
2+
import { defineComponent, h } from 'vue';
3+
4+
import { Article, ArticleResource } from '../../../../__tests__/new';
5+
import useQuery from '../consumers/useQuery';
6+
import useSuspense from '../consumers/useSuspense';
7+
import {
8+
renderDataComposable,
9+
renderDataClient,
10+
} from '../test/renderDataClient';
11+
12+
const GC_INTERVAL = 100; // Use short interval for faster tests
13+
14+
describe('Integration Garbage Collection Web (Vue)', () => {
15+
it('should initialize with GCPolicy', () => {
16+
const gcPolicy = new GCPolicy({
17+
intervalMS: GC_INTERVAL,
18+
expiryMultiplier: 2,
19+
});
20+
expect(gcPolicy).toBeDefined();
21+
});
22+
23+
it('should accept gcPolicy option in renderDataClient', () => {
24+
const TestComp = defineComponent({
25+
name: 'TestComp',
26+
setup() {
27+
return () => h('div', 'Hello');
28+
},
29+
});
30+
31+
const gcPolicy = new GCPolicy({
32+
intervalMS: GC_INTERVAL,
33+
expiryMultiplier: 2,
34+
});
35+
36+
const { wrapper, cleanup } = renderDataClient(TestComp, {
37+
gcPolicy,
38+
});
39+
40+
expect(wrapper.text()).toContain('Hello');
41+
cleanup();
42+
});
43+
44+
it('should work with useSuspense and GCPolicy', async () => {
45+
const articleData = {
46+
id: 1,
47+
title: 'Test Article',
48+
content: 'Test Content',
49+
};
50+
51+
const { result, cleanup, waitForNextUpdate } = renderDataComposable(
52+
() => useSuspense(ArticleResource.get, { id: 1 }),
53+
{
54+
initialFixtures: [
55+
{
56+
endpoint: ArticleResource.get,
57+
args: [{ id: 1 }],
58+
response: articleData,
59+
},
60+
],
61+
gcPolicy: new GCPolicy({
62+
intervalMS: GC_INTERVAL,
63+
expiryMultiplier: 2,
64+
}),
65+
},
66+
);
67+
68+
await waitForNextUpdate();
69+
70+
const articleRef = await result.current;
71+
expect(articleRef?.value.title).toBe(articleData.title);
72+
expect(articleRef?.value.content).toBe(articleData.content);
73+
74+
cleanup();
75+
});
76+
77+
it('should work with useQuery and GCPolicy', () => {
78+
const articleListData = [
79+
{ id: 1, title: 'Article 1', content: 'Content 1' },
80+
{ id: 2, title: 'Article 2', content: 'Content 2' },
81+
];
82+
83+
const { result, cleanup } = renderDataComposable(
84+
() => useQuery(ArticleResource.getList.schema),
85+
{
86+
initialFixtures: [
87+
{
88+
endpoint: ArticleResource.getList,
89+
args: [],
90+
response: articleListData,
91+
},
92+
],
93+
gcPolicy: new GCPolicy({
94+
intervalMS: GC_INTERVAL,
95+
expiryMultiplier: 2,
96+
}),
97+
},
98+
);
99+
100+
expect(result.current?.value).toBeDefined();
101+
expect(result.current?.value?.length).toBe(2);
102+
expect(result.current?.value?.[0]).toBeInstanceOf(Article);
103+
104+
cleanup();
105+
});
106+
107+
it('should handle different expiryMultiplier values', () => {
108+
const articleData = {
109+
id: 1,
110+
title: 'Test Article',
111+
content: 'Test Content',
112+
};
113+
114+
const TestComp = defineComponent({
115+
name: 'TestComp',
116+
setup() {
117+
return () => h('div', 'Test');
118+
},
119+
});
120+
121+
// Test with expiryMultiplier of 4
122+
const { cleanup: cleanup1 } = renderDataClient(TestComp, {
123+
initialFixtures: [
124+
{
125+
endpoint: ArticleResource.get,
126+
args: [{ id: 1 }],
127+
response: articleData,
128+
},
129+
],
130+
gcPolicy: new GCPolicy({
131+
intervalMS: GC_INTERVAL,
132+
expiryMultiplier: 4,
133+
}),
134+
});
135+
136+
cleanup1();
137+
138+
// Test with expiryMultiplier of 2 (default in tests)
139+
const { cleanup: cleanup2 } = renderDataClient(TestComp, {
140+
initialFixtures: [
141+
{
142+
endpoint: ArticleResource.get,
143+
args: [{ id: 1 }],
144+
response: articleData,
145+
},
146+
],
147+
gcPolicy: new GCPolicy({
148+
intervalMS: GC_INTERVAL,
149+
expiryMultiplier: 2,
150+
}),
151+
});
152+
153+
cleanup2();
154+
});
155+
});

packages/vue/src/context.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const FallbackStateRef: ShallowRef<State<unknown>> =
1616
export function useController(): Controller {
1717
const ctrl = inject(ControllerKey, null);
1818
if (!ctrl) {
19+
/* istanbul ignore else */
1920
if (process.env.NODE_ENV !== 'production') {
2021
console.error(
2122
'It appears you are trying to use Reactive Data Client (Vue) without a provider.\n' +
@@ -30,6 +31,7 @@ export function useController(): Controller {
3031
export function injectState(): ShallowRef<State<unknown>> {
3132
const state = inject(StateKey, null);
3233
if (!state) {
34+
/* istanbul ignore else */
3335
if (process.env.NODE_ENV !== 'production') {
3436
console.error(
3537
'It appears you are trying to use Reactive Data Client (Vue) without a provider.\n' +

packages/vue/src/test/collapseFixture.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,3 @@ export async function collapseFixture(
1919
}
2020
return { response, error };
2121
}
22-

packages/vue/src/test/createFixtureMap.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,3 @@ Treating as Interceptor`,
2424
}
2525
return [map, computed] as const;
2626
}
27-

packages/vue/src/test/renderDataClient.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
NetworkManager,
66
SubscriptionManager,
77
PollingSubscription,
8+
type GCInterface,
89
} from '@data-client/core';
910
import { mount, type VueWrapper } from '@vue/test-utils';
1011
import {
@@ -30,6 +31,7 @@ export interface RenderDataClientOptions<P = any> {
3031
getInitialInterceptorData?: () => any;
3132
managers?: Manager[];
3233
initialState?: State<unknown>;
34+
gcPolicy?: GCInterface;
3335
wrapper?: any;
3436
}
3537

@@ -56,6 +58,7 @@ export function renderDataClient<P = any>(
5658
getInitialInterceptorData = () => ({}),
5759
managers,
5860
initialState,
61+
gcPolicy,
5962
wrapper,
6063
} = options;
6164

@@ -133,6 +136,7 @@ export function renderDataClient<P = any>(
133136
managers: finalManagers,
134137
initialState: finalInitialState,
135138
Controller: MockControllerClass,
139+
gcPolicy,
136140
},
137141
],
138142
],

0 commit comments

Comments
 (0)