Skip to content

Commit c0fb170

Browse files
author
Ivan Buryak
committed
- Add PW
- Rename to MiniDb
1 parent 29f561b commit c0fb170

13 files changed

+424
-262
lines changed

.github/workflows/playwright.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Playwright Tests
2+
on:
3+
push:
4+
branches: [ main, master ]
5+
pull_request:
6+
branches: [ main, master ]
7+
jobs:
8+
test:
9+
timeout-minutes: 60
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v3
13+
- uses: actions/setup-node@v3
14+
with:
15+
node-version: 16
16+
- name: Install dependencies
17+
run: npm ci
18+
- name: Install Playwright Browsers
19+
run: npx playwright install --with-deps
20+
- name: Run Playwright tests
21+
run: npx playwright test
22+
- uses: actions/upload-artifact@v3
23+
if: always()
24+
with:
25+
name: playwright-report
26+
path: playwright-report/
27+
retention-days: 30

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ dist-ssr
2222
*.njsproj
2323
*.sln
2424
*.sw?
25+
/test-results/
26+
/playwright-report/
27+
/playwright/.cache/

README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,31 @@ Simple but fully functional way to persist your Jotai atoms in IndexedDB. Analog
1616
// Jotai V2 API!
1717
import { useAtom, useAtomValue, useSetAtom } from "jotai/react";
1818

19-
import { JotaiMiniDb } from "jotai-minidb";
19+
import { MiniDb } from "jotai-minidb";
2020

2121
// Items in a store can be typed
2222
type Item = {
2323
name: string;
2424
};
2525

2626
// Create a store (with default database name)
27-
const myStore = new JotaiMiniDb<Item>();
27+
const myDb = new MiniDb<Item>();
2828

2929
// Multiple stores connected to different databases can be created
30-
const myOtherStore = new JotaiMiniDb<string>({ name: "myOtherStore" })
30+
const myOtherDb = new MiniDb<string>({ name: "myOtherDb" })
3131

3232
// 1. Get all keys, values or entries
3333
export function Entries() {
34-
const keys = useAtomValue(myStore.keys);
35-
const values = useAtomValue(myStore.values);
36-
const entries = useAtomValue(myStore.entries);
34+
const keys = useAtomValue(myDb.keys);
35+
const values = useAtomValue(myDb.values);
36+
const entries = useAtomValue(myDb.entries);
3737

3838
return entries.map(([key, entry]) => <li>{entry.name}</li>)
3939
}
4040

4141
// 2. Get, or set item in a store
4242
export function Entry() {
43-
const [item, setItem] = useAtom(myStore.item('some-item-key'));
43+
const [item, setItem] = useAtom(myDb.item('some-item-key'));
4444

4545
if (!item) {
4646
return null
@@ -74,7 +74,7 @@ type Item = {
7474
age: number;
7575
};
7676

77-
const myStore = new JotaiMiniDb<Item>({
77+
const myDb = new MiniDb<Item>({
7878
version: 1,
7979
migrations: {
8080
1: (item) => {

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"type": "module",
77
"license": "MIT",
88
"scripts": {
9-
"dev": "vite",
9+
"dev": "vite --port=4141",
1010
"build": "tsc && vite build",
1111
"preview": "vite preview",
1212
"test": "vitest"
@@ -24,6 +24,7 @@
2424
"jotai": ">=1.10.0"
2525
},
2626
"devDependencies": {
27+
"@playwright/test": "^1.29.1",
2728
"@types/react": "^18.0.26",
2829
"@types/react-dom": "^18.0.9",
2930
"@vitejs/plugin-react": "^3.0.0",

playwright.config.ts

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import type { PlaywrightTestConfig } from '@playwright/test';
2+
import { devices } from '@playwright/test';
3+
4+
/**
5+
* Read environment variables from file.
6+
* https://github.com/motdotla/dotenv
7+
*/
8+
// require('dotenv').config();
9+
10+
/**
11+
* See https://playwright.dev/docs/test-configuration.
12+
*/
13+
const config: PlaywrightTestConfig = {
14+
testDir: './tests',
15+
/* Maximum time one test can run for. */
16+
timeout: 30 * 1000,
17+
expect: {
18+
/**
19+
* Maximum time expect() should wait for the condition to be met.
20+
* For example in `await expect(locator).toHaveText();`
21+
*/
22+
timeout: 5000
23+
},
24+
/* Run tests in files in parallel */
25+
fullyParallel: true,
26+
/* Fail the build on CI if you accidentally left test.only in the source code. */
27+
forbidOnly: !!process.env.CI,
28+
/* Retry on CI only */
29+
retries: process.env.CI ? 2 : 0,
30+
/* Opt out of parallel tests on CI. */
31+
workers: process.env.CI ? 1 : undefined,
32+
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
33+
reporter: 'html',
34+
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
35+
use: {
36+
/* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */
37+
actionTimeout: 0,
38+
/* Base URL to use in actions like `await page.goto('/')`. */
39+
baseURL: 'http://localhost:4141',
40+
41+
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
42+
trace: 'on-first-retry',
43+
},
44+
45+
/* Configure projects for major browsers */
46+
projects: [
47+
{
48+
name: 'chromium',
49+
use: {
50+
...devices['Desktop Chrome'],
51+
},
52+
},
53+
54+
// {
55+
// name: 'firefox',
56+
// use: {
57+
// ...devices['Desktop Firefox'],
58+
// },
59+
// },
60+
61+
// {
62+
// name: 'webkit',
63+
// use: {
64+
// ...devices['Desktop Safari'],
65+
// },
66+
// },
67+
68+
/* Test against mobile viewports. */
69+
// {
70+
// name: 'Mobile Chrome',
71+
// use: {
72+
// ...devices['Pixel 5'],
73+
// },
74+
// },
75+
// {
76+
// name: 'Mobile Safari',
77+
// use: {
78+
// ...devices['iPhone 12'],
79+
// },
80+
// },
81+
82+
/* Test against branded browsers. */
83+
// {
84+
// name: 'Microsoft Edge',
85+
// use: {
86+
// channel: 'msedge',
87+
// },
88+
// },
89+
// {
90+
// name: 'Google Chrome',
91+
// use: {
92+
// channel: 'chrome',
93+
// },
94+
// },
95+
],
96+
97+
/* Folder for test artifacts such as screenshots, videos, traces, etc. */
98+
// outputDir: 'test-results/',
99+
100+
/* Run your local dev server before starting the tests */
101+
webServer: {
102+
command: 'yarn dev',
103+
port: 4141,
104+
reuseExistingServer: !process.env.CI,
105+
},
106+
};
107+
108+
export default config;

src/Showcase.css

-5
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ a:hover {
2626

2727
body {
2828
margin: 0;
29-
display: flex;
30-
place-items: center;
31-
min-width: 320px;
3229
min-height: 100vh;
3330
}
3431

@@ -70,9 +67,7 @@ button:focus-visible {
7067
}
7168
#root {
7269
max-width: 1280px;
73-
margin: 0 auto;
7470
padding: 2rem;
75-
text-align: center;
7671
}
7772

7873
.logo {

src/Showcase.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { useState } from "react";
22
import { useAtom, useAtomValue, useSetAtom } from "jotai/react";
33

4-
import { JotaiMiniDb } from "./lib/jotai-minidb";
4+
import { MiniDb } from "./lib/jotai-minidb";
55

66
type Item = {
77
name: string;
88
};
9-
const simpleStore = new JotaiMiniDb<Item>();
9+
const simpleStore = new MiniDb<Item>();
1010

1111
function getNewItem(): Item {
1212
return {
@@ -15,13 +15,15 @@ function getNewItem(): Item {
1515
}
1616

1717
export default function Showcase() {
18+
useAtomValue(simpleStore.initStatus);
1819
const [selectedId, setSelectedId] = useState<string | null>(null);
1920
const entries = useAtomValue(simpleStore.entries);
2021
const set = useSetAtom(simpleStore.set);
2122
const del = useSetAtom(simpleStore.delete);
2223

2324
return (
2425
<div className="container">
26+
<h1>Jotai-minidb example app</h1>
2527
<div className="sidebar">
2628
<ul>
2729
{entries.map(([key, item]) => (

src/lib/jotai-minidb.test-d.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
import { WritableAtom, Atom } from "jotai/vanilla";
22
import { it, expectTypeOf } from "vitest";
3-
import { JotaiMiniDb } from "./jotai-minidb";
3+
import { MiniDb } from "./jotai-minidb";
44

55
type Item = {
66
my: string;
77
prop: number;
88
};
99

1010
it("item atom typings", () => {
11-
const db = new JotaiMiniDb<Item>();
11+
const db = new MiniDb<Item>();
1212
expectTypeOf(db.item("123")).toMatchTypeOf<
1313
WritableAtom<Item | undefined, [Item], Promise<void>>
1414
>();
1515
});
1616

1717
it("entries atom typings", () => {
18-
const db = new JotaiMiniDb<Item>();
18+
const db = new MiniDb<Item>();
1919
expectTypeOf(db.entries).toMatchTypeOf<Atom<[string, Item][]>>();
2020
});
2121

2222
it("keys atom typings", () => {
23-
const db = new JotaiMiniDb<Item>();
23+
const db = new MiniDb<Item>();
2424
expectTypeOf(db.keys).toMatchTypeOf<Atom<string[]>>();
2525
});
2626

2727
it("values atom typings", () => {
28-
const db = new JotaiMiniDb<Item>();
28+
const db = new MiniDb<Item>();
2929
expectTypeOf(db.values).toMatchTypeOf<Atom<Item[]>>();
3030
});

0 commit comments

Comments
 (0)