generated from react18-tools/turborepo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from mayank1513:test
Test
- Loading branch information
Showing
12 changed files
with
202 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
name: Publish to NPM | ||
|
||
# publish only when package json has changed - assuming version upgrade | ||
on: | ||
push: | ||
branches: [main] | ||
paths: "packages/persist-and-sync/package.json" | ||
|
||
jobs: | ||
publish: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
packages: write | ||
contents: write | ||
|
||
defaults: | ||
run: | ||
working-directory: ./packages/persist-and-sync | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
token: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- uses: actions/setup-node@v3 | ||
with: | ||
registry-url: https://registry.npmjs.org | ||
node-version: 18 | ||
- run: npm i -g pnpm && pnpm i | ||
name: Install dependencies | ||
# fail and not publish if any of the unit tests are failing | ||
- name: Test | ||
run: pnpm test | ||
- name: Publish to NPM | ||
run: pnpm build && pnpm publish-package | ||
env: | ||
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
name: test | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
schedule: | ||
- cron: "0 8 * * *" | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: 18 | ||
- run: npm i -g pnpm && pnpm i | ||
name: Install dependencies | ||
- name: Run unit tests | ||
run: pnpm test | ||
- name: Upload coverage reports to Codecov | ||
uses: codecov/codecov-action@v3 | ||
with: | ||
directory: ./packages/persist-and-sync | ||
token: ${{ secrets.CODECOV_TOKEN }} | ||
flags: fork-me | ||
- uses: paambaati/codeclimate-action@v5.0.0 | ||
env: | ||
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }} | ||
with: | ||
coverageLocations: ./packages/persist-and-sync/coverage/*.xml:clover |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { create } from "zustand"; | ||
import { persistNSync } from "persist-and-sync"; | ||
|
||
interface MyStoreType { | ||
count: number; | ||
_count: number; | ||
setCount: (c: number) => void; | ||
set_Count: (c: number) => void; | ||
} | ||
|
||
export const useMyStore = create<MyStoreType>()( | ||
persistNSync( | ||
set => ({ | ||
count: 0, | ||
_count: 0 /** skipped as it matches the regexp provided */, | ||
setCount: count => { | ||
set(state => ({ ...state, count })); | ||
}, | ||
set_Count: _count => { | ||
set(state => ({ ...state, _count })); | ||
}, | ||
}), | ||
{ name: "example", regExpToIgnore: /^_/ }, | ||
), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { act, cleanup, renderHook } from "@testing-library/react"; | ||
|
||
import { afterEach, describe, test } from "vitest"; | ||
import { useMyStore } from "./store"; | ||
|
||
describe.concurrent("Setting state", () => { | ||
afterEach(cleanup); | ||
test("test initial state", async ({ expect }) => { | ||
const { result } = renderHook(() => useMyStore()); | ||
expect(result.current.count).toBe(0); | ||
}); | ||
|
||
test("test setting state", async ({ expect }) => { | ||
const { result } = renderHook(() => useMyStore()); | ||
act(() => result.current.setCount(5)); | ||
expect(result.current.count).toBe(5); | ||
expect(localStorage.getItem("example")).toBe('{"count":5}'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { act } from "@testing-library/react"; | ||
import { afterEach, vi } from "vitest"; | ||
import type { StateCreator } from "zustand"; | ||
import { create as actualCreate } from "zustand"; | ||
|
||
// a variable to hold reset functions for all stores declared in the app | ||
export const storeResetFns = new Set<() => void>(); | ||
|
||
// when creating a store, we get its initial state, create a reset function and add it in the set | ||
export const create = <S>(createState: StateCreator<S>) => { | ||
const store = actualCreate(createState); | ||
const initialState = store.getState(); | ||
storeResetFns.add(() => store.setState(initialState, true)); | ||
return store; | ||
}; | ||
|
||
afterEach(() => { | ||
act(() => storeResetFns.forEach(resetFn => resetFn())); | ||
}); | ||
|
||
declare global { | ||
var tmp_store: { [key: string]: string }; | ||
} | ||
|
||
globalThis.tmp_store = {}; | ||
|
||
globalThis.localStorage = { | ||
length: Object.keys(tmp_store).length, | ||
clear: () => { | ||
tmp_store = {}; | ||
}, | ||
key: (index: number) => Object.keys(tmp_store)[index], | ||
removeItem: (key: string) => { | ||
delete tmp_store[key]; | ||
}, | ||
setItem: (key: string, item: string) => { | ||
tmp_store[key] = item; | ||
}, | ||
getItem: (key: string) => tmp_store[key], | ||
}; | ||
|
||
function channelMock() {} | ||
channelMock.prototype.onmessage = function () {}; | ||
channelMock.prototype.postMessage = function (data) { | ||
this.onmessage({ data }); | ||
}; | ||
// @ts-ignore | ||
global.BroadcastChannel = channelMock; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { defineConfig } from "vitest/config"; | ||
import react from "@vitejs/plugin-react"; | ||
|
||
// https://vitejs.dev/config/ | ||
// eslint-disable-next-line import/no-default-export -- export default is required for config files | ||
export default defineConfig({ | ||
plugins: [react()], | ||
test: { | ||
environment: "jsdom", | ||
globals: true, | ||
setupFiles: ["vitest-setup.ts"], | ||
coverage: { | ||
reporter: ["text", "json", "clover", "html"], | ||
}, | ||
threads: true, | ||
mockReset: false, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
"persist-and-sync#build": { | ||
"cache": false | ||
}, | ||
"test": {}, | ||
"lint": {}, | ||
"dev": { | ||
"cache": false, | ||
|