react-smart-query 1.0.0
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.
- package/LICENSE +21 -0
- package/README.md +130 -0
- package/dist/cache.service-MR6EEYM4.mjs +4 -0
- package/dist/cache.service-MR6EEYM4.mjs.map +1 -0
- package/dist/chunk-KLJQATIV.mjs +170 -0
- package/dist/chunk-KLJQATIV.mjs.map +1 -0
- package/dist/chunk-KSLDOL27.mjs +133 -0
- package/dist/chunk-KSLDOL27.mjs.map +1 -0
- package/dist/chunk-QRCVY7UR.mjs +137 -0
- package/dist/chunk-QRCVY7UR.mjs.map +1 -0
- package/dist/index.d.mts +545 -0
- package/dist/index.d.ts +545 -0
- package/dist/index.js +1533 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1018 -0
- package/dist/index.mjs.map +1 -0
- package/dist/storage.adapter-PJCVI4DE.mjs +3 -0
- package/dist/storage.adapter-PJCVI4DE.mjs.map +1 -0
- package/dist/testing.d.mts +89 -0
- package/dist/testing.d.ts +89 -0
- package/dist/testing.js +272 -0
- package/dist/testing.js.map +1 -0
- package/dist/testing.mjs +78 -0
- package/dist/testing.mjs.map +1 -0
- package/dist/types-XXiTKLnh.d.mts +134 -0
- package/dist/types-XXiTKLnh.d.ts +134 -0
- package/dist/utils/debug.d.mts +2 -0
- package/dist/utils/debug.d.ts +2 -0
- package/dist/utils/debug.js +208 -0
- package/dist/utils/debug.js.map +1 -0
- package/dist/utils/debug.mjs +40 -0
- package/dist/utils/debug.mjs.map +1 -0
- package/docs/API_REFERENCE.md +149 -0
- package/docs/GUIDELINES.md +23 -0
- package/docs/TESTING.md +61 -0
- package/package.json +136 -0
package/docs/TESTING.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# Testing & Debugging with React Smart Query
|
|
2
|
+
|
|
3
|
+
Testing offline-first normalized data layers can be tricky, but `react-smart-query` provides dedicated utilities to make it painless.
|
|
4
|
+
|
|
5
|
+
## 1. Setting up the Test Environment
|
|
6
|
+
|
|
7
|
+
When testing components that use `useSmartQuery` or `useInfiniteSmartQuery`, you should wrap them in the `SmartQueryTestProvider` rather than standard QueryClientProviders. This ensures the internal registry and normalization engines are securely isolated per test.
|
|
8
|
+
|
|
9
|
+
### Installation
|
|
10
|
+
Make sure you import from the designated `testing` entry point, ensuring test tools aren't bundled into production:
|
|
11
|
+
```tsx
|
|
12
|
+
import { render } from '@testing-library/react-native';
|
|
13
|
+
import { SmartQueryTestProvider } from 'react-smart-query/testing';
|
|
14
|
+
|
|
15
|
+
const renderWithProvider = (ui: React.ReactElement) => {
|
|
16
|
+
return render(
|
|
17
|
+
<SmartQueryTestProvider>
|
|
18
|
+
{ui}
|
|
19
|
+
</SmartQueryTestProvider>
|
|
20
|
+
);
|
|
21
|
+
};
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## 2. Seeding Data
|
|
25
|
+
Oftentimes, you want to test how a component behaves *given* some pre-existing cached state, without mocking the network layer heavily. You can seed the cache directly inside your tests using the `seedCache` utility.
|
|
26
|
+
|
|
27
|
+
```tsx
|
|
28
|
+
import { SmartQueryTestProvider, seedCache } from 'react-smart-query/testing';
|
|
29
|
+
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
seedCache(['expenses', 'user-123'], [
|
|
32
|
+
{ id: '1', amount: 50, desc: 'Coffee' },
|
|
33
|
+
{ id: '2', amount: 120, desc: 'Groceries' }
|
|
34
|
+
]);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('renders expenses from cache directly', () => {
|
|
38
|
+
const { getByText } = renderWithProvider(<ExpenseList userId="user-123" />);
|
|
39
|
+
expect(getByText('Coffee')).toBeTruthy();
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## 3. The Debugger
|
|
44
|
+
If you ever find yourself wondering exactly what's sitting in the `react-smart-query` internal normalization maps, you can use the built in debugger. We highly recommend only tying this to `__DEV__`.
|
|
45
|
+
|
|
46
|
+
### Activating the Debugger
|
|
47
|
+
In your root file (like App.tsx):
|
|
48
|
+
```tsx
|
|
49
|
+
if (__DEV__) {
|
|
50
|
+
require('react-smart-query/debug');
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Retrieving Snapshots
|
|
55
|
+
Once activated, you can trigger snapshots to the console representing the current entire data map and state.
|
|
56
|
+
```tsx
|
|
57
|
+
import { smartQueryDebug } from 'react-smart-query';
|
|
58
|
+
|
|
59
|
+
// From a component effect, or attached to a dev-only button:
|
|
60
|
+
await smartQueryDebug.snapshot();
|
|
61
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-smart-query",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Offline-first, normalized, cache-versioned data layer for React Native and Expo. Built on TanStack Query with MMKV (native) and IndexedDB (web).",
|
|
5
|
+
"author": "Subham Ghosh <subham49ghosh@gmail.com>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/subham067/react-smart-query.git"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/subham067/react-smart-query#readme",
|
|
12
|
+
"keywords": [
|
|
13
|
+
"react-native",
|
|
14
|
+
"expo",
|
|
15
|
+
"offline-first",
|
|
16
|
+
"cache",
|
|
17
|
+
"tanstack-query",
|
|
18
|
+
"react-query",
|
|
19
|
+
"mmkv",
|
|
20
|
+
"indexeddb",
|
|
21
|
+
"normalized",
|
|
22
|
+
"typescript"
|
|
23
|
+
],
|
|
24
|
+
"main": "./dist/index.js",
|
|
25
|
+
"module": "./dist/index.mjs",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"import": "./dist/index.mjs",
|
|
31
|
+
"require": "./dist/index.js"
|
|
32
|
+
},
|
|
33
|
+
"./testing": {
|
|
34
|
+
"types": "./dist/testing.d.ts",
|
|
35
|
+
"import": "./dist/testing.mjs",
|
|
36
|
+
"require": "./dist/testing.js"
|
|
37
|
+
},
|
|
38
|
+
"./debug": {
|
|
39
|
+
"types": "./dist/utils/debug.d.ts",
|
|
40
|
+
"import": "./dist/utils/debug.mjs",
|
|
41
|
+
"require": "./dist/utils/debug.js"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"files": [
|
|
45
|
+
"dist/",
|
|
46
|
+
"docs/",
|
|
47
|
+
"README.md",
|
|
48
|
+
"LICENSE"
|
|
49
|
+
],
|
|
50
|
+
"sideEffects": [
|
|
51
|
+
"./dist/utils/debug.js",
|
|
52
|
+
"./dist/utils/debug.mjs"
|
|
53
|
+
],
|
|
54
|
+
"scripts": {
|
|
55
|
+
"build": "tsup src/index.ts src/testing.ts src/utils/debug.ts --format cjs,esm --dts --clean",
|
|
56
|
+
"test": "jest --passWithNoTests",
|
|
57
|
+
"test:watch": "jest --watch",
|
|
58
|
+
"test:coverage": "jest --coverage",
|
|
59
|
+
"typecheck": "tsc --noEmit",
|
|
60
|
+
"lint": "eslint src --ext .ts,.tsx",
|
|
61
|
+
"prepublishOnly": "npm run typecheck && npm run build"
|
|
62
|
+
},
|
|
63
|
+
"peerDependencies": {
|
|
64
|
+
"@tanstack/react-query": "^5.0.0",
|
|
65
|
+
"fast-deep-equal": ">=3.0.0",
|
|
66
|
+
"react": ">=18.0.0",
|
|
67
|
+
"react-native": ">=0.72.0",
|
|
68
|
+
"react-native-mmkv": ">=2.0.0"
|
|
69
|
+
},
|
|
70
|
+
"peerDependenciesMeta": {
|
|
71
|
+
"react-native": {
|
|
72
|
+
"optional": true
|
|
73
|
+
},
|
|
74
|
+
"react-native-mmkv": {
|
|
75
|
+
"optional": true
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
"devDependencies": {
|
|
79
|
+
"@tanstack/react-query": "^5.96.2",
|
|
80
|
+
"@testing-library/react-native": "^12.0.0",
|
|
81
|
+
"@types/jest": "^29.0.0",
|
|
82
|
+
"@types/react": "^18.3.28",
|
|
83
|
+
"@types/react-native": "^0.72.8",
|
|
84
|
+
"fast-deep-equal": "^3.1.3",
|
|
85
|
+
"jest": "^29.0.0",
|
|
86
|
+
"jest-expo": "^50.0.0",
|
|
87
|
+
"react": "^19.2.4",
|
|
88
|
+
"react-native": ">=0.72.0",
|
|
89
|
+
"react-native-mmkv": "^4.3.0",
|
|
90
|
+
"babel-jest": "^29.0.0",
|
|
91
|
+
"babel-preset-expo": "~54.0.0",
|
|
92
|
+
"ts-jest": "^29.0.0",
|
|
93
|
+
"tsup": "^8.0.0",
|
|
94
|
+
"typescript": "^5.0.0"
|
|
95
|
+
},
|
|
96
|
+
"jest": {
|
|
97
|
+
"preset": "react-native",
|
|
98
|
+
"transform": {
|
|
99
|
+
"^.+\\.[jt]sx?$": "babel-jest"
|
|
100
|
+
},
|
|
101
|
+
"transformIgnorePatterns": [
|
|
102
|
+
"node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg)"
|
|
103
|
+
],
|
|
104
|
+
"testMatch": [
|
|
105
|
+
"**/__tests__/**/*.test.[jt]s?(x)"
|
|
106
|
+
],
|
|
107
|
+
"setupFilesAfterEnv": [
|
|
108
|
+
"@testing-library/react-native/extend-expect"
|
|
109
|
+
],
|
|
110
|
+
"moduleNameMapper": {
|
|
111
|
+
"^react-native-mmkv$": "<rootDir>/__mocks__/react-native-mmkv.ts"
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
"tsup": {
|
|
115
|
+
"entry": [
|
|
116
|
+
"src/index.ts",
|
|
117
|
+
"src/testing.ts",
|
|
118
|
+
"src/utils/debug.ts"
|
|
119
|
+
],
|
|
120
|
+
"format": [
|
|
121
|
+
"cjs",
|
|
122
|
+
"esm"
|
|
123
|
+
],
|
|
124
|
+
"dts": true,
|
|
125
|
+
"clean": true,
|
|
126
|
+
"sourcemap": true,
|
|
127
|
+
"treeshake": true,
|
|
128
|
+
"external": [
|
|
129
|
+
"react",
|
|
130
|
+
"react-native",
|
|
131
|
+
"@tanstack/react-query",
|
|
132
|
+
"react-native-mmkv",
|
|
133
|
+
"fast-deep-equal"
|
|
134
|
+
]
|
|
135
|
+
}
|
|
136
|
+
}
|