Skip to content

Commit f1bad11

Browse files
committed
fix: makeObservable field support old decorators for inheritance cases; ci/cd: add gh jobs; test: add simple unit tests for mobx query
1 parent c133d04 commit f1bad11

File tree

6 files changed

+97
-37
lines changed

6 files changed

+97
-37
lines changed

.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ jsconfig.json
1515
*.json
1616
.eslintrc.cjs
1717
post-build.mjs
18-
coverage
18+
coverage
19+
vitest.config.ts

.github/workflows/build.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Build
2+
on:
3+
push:
4+
branches:
5+
- master
6+
7+
jobs:
8+
job:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v4
14+
15+
- uses: pnpm/action-setup@v4
16+
name: Install pnpm
17+
with:
18+
run_install: false
19+
20+
- name: Install Node.js
21+
uses: actions/setup-node@v4
22+
with:
23+
node-version: 20
24+
cache: 'pnpm'
25+
26+
- name: Install dependencies
27+
run: pnpm install
28+
29+
- name: Build the project
30+
run: pnpm build

src/mobx-inifinite-query.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,13 @@ export class MobxInfiniteQuery<
140140
disposer.add(() => this.dispose());
141141
}
142142

143-
makeObservable<this, 'updateResult'>(this, {
144-
_result: observable.ref,
145-
setData: action.bound,
146-
update: action.bound,
147-
updateResult: action.bound,
148-
});
143+
observable.ref(this, '_result');
144+
observable.ref(this, 'isResultRequsted');
145+
action.bound(this, 'setData');
146+
action.bound(this, 'update');
147+
action.bound(this, 'updateResult');
148+
149+
makeObservable(this);
149150

150151
const mergedOptions = {
151152
...options,

src/mobx-mutation.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,10 @@ export class MobxMutation<
6262
disposer.add(() => this.dispose());
6363
}
6464

65-
makeObservable<this, 'updateResult'>(
66-
this,
67-
{
68-
result: observable.ref,
69-
updateResult: action.bound,
70-
},
71-
{ deep: false },
72-
);
65+
observable.ref(this, 'result');
66+
action.bound(this, 'updateResult');
67+
68+
makeObservable(this);
7369

7470
this.mutationOptions = this.queryClient.defaultMutationOptions(options);
7571

src/mobx-query.test.ts

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,53 @@
1-
import { describe, expect, it } from 'vitest';
2-
import { MobxQuery } from './mobx-query';
3-
import { QueryClient } from '@tanstack/query-core';
1+
import { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core';
2+
import { reaction, when } from 'mobx';
3+
import { describe, expect, it, vi } from 'vitest';
44

5+
import { MobxQuery, MobxQueryConfig } from './mobx-query';
56

67
describe('MobxQuery', () => {
8+
const testQueryClient = new QueryClient({});
9+
class TestMobxQuery<
10+
TData,
11+
TError = DefaultError,
12+
TQueryKey extends QueryKey = any,
13+
> extends MobxQuery<TData, TError, TQueryKey> {
14+
constructor(
15+
options: Omit<MobxQueryConfig<TData, TError, TQueryKey>, 'queryClient'>,
16+
) {
17+
super({ ...options, queryClient: testQueryClient });
18+
}
19+
}
20+
721
it('to be defined', () => {
8-
const mobxQuery = new MobxQuery({
9-
queryClient: new QueryClient(),
22+
const mobxQuery = new TestMobxQuery({
1023
queryKey: ['test'],
11-
queryFn: () => {}
12-
})
24+
queryFn: () => {},
25+
});
1326
expect(mobxQuery).toBeDefined();
14-
})
15-
});
27+
});
28+
29+
it('"result" field should be reactive', async () => {
30+
let counter = 0;
31+
const mobxQuery = new TestMobxQuery({
32+
queryKey: ['test'],
33+
queryFn: () => {
34+
return ++counter;
35+
},
36+
});
37+
const spy = vi.fn();
38+
39+
const dispose = reaction(
40+
() => mobxQuery.result,
41+
(result) => {
42+
spy(result.data);
43+
},
44+
);
45+
46+
await when(() => mobxQuery.result.isLoading);
47+
48+
expect(spy).toBeCalledTimes(1);
49+
expect(spy).nthCalledWith(1, 1);
50+
51+
dispose();
52+
});
53+
});

src/mobx-query.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,13 @@ export class MobxQuery<
106106
disposer.add(() => this.dispose());
107107
}
108108

109-
makeObservable<this, 'updateResult' | '_result'>(
110-
this,
111-
{
112-
_result: observable.ref,
113-
isResultRequsted: observable.ref,
114-
setData: action.bound,
115-
update: action.bound,
116-
updateResult: action.bound,
117-
},
118-
{
119-
deep: false,
120-
},
121-
);
109+
observable.ref(this, '_result');
110+
observable.ref(this, 'isResultRequsted');
111+
action.bound(this, 'setData');
112+
action.bound(this, 'update');
113+
action.bound(this, 'updateResult');
114+
115+
makeObservable(this);
122116

123117
const mergedOptions = {
124118
...options,

0 commit comments

Comments
 (0)