Skip to content

Commit 00d9891

Browse files
committed
add type tests to onyx!
1 parent 70f769d commit 00d9891

6 files changed

Lines changed: 64 additions & 2 deletions

File tree

.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*.d.ts
22
dist
33
node_modules
4-
*.config.js
4+
*.config.js
5+
tests/types/**/*.ts

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ jobs:
3030
- run: npm run test
3131
env:
3232
CI: true
33+
34+
- run: npm run test:types

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"lint": "eslint .",
3333
"typecheck": "tsc --noEmit",
3434
"test": "jest",
35+
"test:types": "npm run build && tsc --noEmit --project tsconfig.test.json",
3536
"perf-test": "npx reassure",
3637
"build": "tsc -p tsconfig.build.json",
3738
"build:watch": "nodemon --watch lib --ext js,json,ts,tsx --exec \"npm run build && npm pack\"",

tests/types/test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import type {OnyxUpdate} from '../../dist/types';
2+
3+
const ONYX_KEYS = {
4+
TEST_KEY: 'test',
5+
COLLECTION: {
6+
TEST_KEY: 'test_',
7+
},
8+
};
9+
10+
type OnyxValues = {
11+
[ONYX_KEYS.TEST_KEY]: string;
12+
};
13+
14+
type OnyxCollectionValues = {
15+
[ONYX_KEYS.COLLECTION.TEST_KEY]: {str: string};
16+
};
17+
18+
declare module '../../dist/types' {
19+
interface CustomTypeOptions {
20+
keys: keyof OnyxValues;
21+
collectionKeys: keyof OnyxCollectionValues;
22+
values: OnyxValues;
23+
}
24+
}
25+
26+
const onyxUpdate: OnyxUpdate = {
27+
onyxMethod: 'set',
28+
key: ONYX_KEYS.TEST_KEY,
29+
value: 'string',
30+
};
31+
32+
const onyxUpdateError: OnyxUpdate = {
33+
onyxMethod: 'set',
34+
key: 'string',
35+
// @ts-expect-error TEST_KEY is a string, not a number
36+
value: 2,
37+
};
38+
39+
const onyxUpdateCollection: OnyxUpdate = {
40+
onyxMethod: 'mergecollection',
41+
key: ONYX_KEYS.COLLECTION.TEST_KEY,
42+
value: {
43+
[ONYX_KEYS.COLLECTION.TEST_KEY]: {str: 'test'},
44+
},
45+
};
46+
47+
const onyxUpdateCollectionError: OnyxUpdate = {
48+
onyxMethod: 'mergecollection',
49+
key: ONYX_KEYS.COLLECTION.TEST_KEY,
50+
// @ts-expect-error TEST_KEY is an object, not a number
51+
value: 2,
52+
};

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@
1717
"declaration": true,
1818
"outDir": "./dist"
1919
},
20-
"exclude": ["**/node_modules/**/*", "**/dist/**/*"]
20+
"exclude": ["**/node_modules/**/*", "**/dist/**/*", "tests/types/**/*.ts"]
2121
}

tsconfig.test.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"$schema": "https://json.schemastore.org/tsconfig",
3+
"extends": "./tsconfig.json",
4+
"include": ["tests/types/**/*.ts"],
5+
"exclude": ["**/node_modules/**/*", "lib/**/*"]
6+
}

0 commit comments

Comments
 (0)