Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Refactor to use the new jotai store derive method (Attempt 2) #56

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }],
"@typescript-eslint/no-non-null-asserted-optional-chain": "off",
"@typescript-eslint/no-unused-vars": ["warn", { "argsIgnorePattern": "^_" }],
"react/button-has-type": "off",
"react/jsx-filename-extension": ["error", { "extensions": [".js", ".tsx"] }],
"react/prop-types": "off",
Expand All @@ -41,6 +42,7 @@
"symbol-description": "off",
"prefer-object-spread": "off",
"no-return-assign": "off",
"no-sparse-arrays": "off",
"no-use-before-define": "off",
"no-unused-vars": "off",
"no-redeclare": "off",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
type SetStateAction,
} from 'jotai'
import { atomWithReducer } from 'jotai/vanilla/utils'
import { ScopeProvider } from '../../src/index'
import { clickButton, getTextContents } from '../utils'
import { ScopeProvider } from 'src/ScopeProvider2/ScopeProvider'
import { clickButton, getTextContents } from './utils'

describe('Counter', () => {
/*
Expand Down Expand Up @@ -408,7 +408,7 @@ describe('Counter', () => {
})

/*
base, derivedA(base), derivemB(base)
base, derivedA(base), derivedB(base)
S0[derivedA, derivedB]: derivedA0(base0), derivedB0(base0)
S1[derivedA, derivedB]: derivedA1(base1), derivedB1(base1)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { render } from '@testing-library/react'
import type { PropsWithChildren } from 'react'
import { atom, useAtom, useAtomValue } from 'jotai'
import { atomWithReducer } from 'jotai/vanilla/utils'
import { ScopeProvider } from '../../src/index'
import { clickButton, getTextContents } from '../utils'
import { ScopeProvider } from 'src/ScopeProvider2/ScopeProvider'
import { clickButton, getTextContents } from './utils'

const baseAtom1 = atomWithReducer(0, (v) => v + 1)
const baseAtom2 = atomWithReducer(0, (v) => v + 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { render } from '@testing-library/react'
import { atom, useAtom, useAtomValue, useSetAtom } from 'jotai'
import { atomWithReducer } from 'jotai/vanilla/utils'
import { clickButton, getTextContents } from '../utils'
import { clickButton, getTextContents } from './utils'

import { ScopeProvider } from '../../src/index'
import { ScopeProvider } from 'src/ScopeProvider2/ScopeProvider'

Check failure on line 6 in __tests__/ScopeProvider2/03_nested.test.tsx

View workflow job for this annotation

GitHub Actions / test

`src/ScopeProvider2/ScopeProvider` import should occur before import of `./utils`

Check failure on line 6 in __tests__/ScopeProvider2/03_nested.test.tsx

View workflow job for this annotation

GitHub Actions / test

`src/ScopeProvider2/ScopeProvider` import should occur before import of `./utils`

const baseAtom1 = atomWithReducer(0, (v) => v + 1)
const baseAtom2 = atomWithReducer(0, (v) => v + 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { render } from '@testing-library/react'
import { atom, useAtom } from 'jotai'
import { clickButton, getTextContents } from '../utils'
import { ScopeProvider } from '../../src/index'
import { clickButton, getTextContents } from './utils'
import { ScopeProvider } from 'src/ScopeProvider2/ScopeProvider'

Check failure on line 4 in __tests__/ScopeProvider2/04_derived.test.tsx

View workflow job for this annotation

GitHub Actions / test

`src/ScopeProvider2/ScopeProvider` import should occur before import of `./utils`

Check failure on line 4 in __tests__/ScopeProvider2/04_derived.test.tsx

View workflow job for this annotation

GitHub Actions / test

`src/ScopeProvider2/ScopeProvider` import should occur before import of `./utils`

const atomValueSelectors = [
'.case1.base',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { render } from '@testing-library/react'
import { atom, useAtom } from 'jotai'
import { useHydrateAtoms } from 'jotai/utils'
import { getTextContents } from '../utils'
import { ScopeProvider } from '../../src/index'
import { getTextContents } from './utils'
import { ScopeProvider } from 'src/ScopeProvider2/ScopeProvider'

Check failure on line 5 in __tests__/ScopeProvider2/05_derived_self.test.tsx

View workflow job for this annotation

GitHub Actions / test

`src/ScopeProvider2/ScopeProvider` import should occur before import of `./utils`

Check failure on line 5 in __tests__/ScopeProvider2/05_derived_self.test.tsx

View workflow job for this annotation

GitHub Actions / test

`src/ScopeProvider2/ScopeProvider` import should occur before import of `./utils`

const baseAtom = atom(0)
const derivedAtom1 = atom(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import { render } from '@testing-library/react'
import { atom, useAtom, useAtomValue } from 'jotai'
import { atomWithReducer } from 'jotai/vanilla/utils'
import { clickButton, getTextContents } from '../utils'
import { ScopeProvider } from '../../src/index'
import { clickButton, getTextContents } from './utils'
import { ScopeProvider } from 'src/ScopeProvider2/ScopeProvider'

Check failure on line 6 in __tests__/ScopeProvider2/06_implicit_parent.test.tsx

View workflow job for this annotation

GitHub Actions / test

`src/ScopeProvider2/ScopeProvider` import should occur before import of `./utils`

Check failure on line 6 in __tests__/ScopeProvider2/06_implicit_parent.test.tsx

View workflow job for this annotation

GitHub Actions / test

`src/ScopeProvider2/ScopeProvider` import should occur before import of `./utils`

function renderWithOrder(level1: 'BD' | 'DB', level2: 'BD' | 'DB') {
const baseAtom = atomWithReducer(0, (v) => v + 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { render } from '@testing-library/react'
import { type WritableAtom, type PrimitiveAtom, atom, useAtom } from 'jotai'
import { clickButton, getTextContents } from '../utils'
import { ScopeProvider } from '../../src/index'
import { clickButton, getTextContents } from './utils'
import { ScopeProvider } from 'src/ScopeProvider2/ScopeProvider'

Check failure on line 4 in __tests__/ScopeProvider2/07_writable.test.tsx

View workflow job for this annotation

GitHub Actions / test

`src/ScopeProvider2/ScopeProvider` import should occur before import of `./utils`

Check failure on line 4 in __tests__/ScopeProvider2/07_writable.test.tsx

View workflow job for this annotation

GitHub Actions / test

`src/ScopeProvider2/ScopeProvider` import should occur before import of `./utils`

let baseAtom: PrimitiveAtom<number>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { render, act } from '@testing-library/react'
import { useAtom, atom, useSetAtom } from 'jotai'
import { atomFamily, atomWithReducer } from 'jotai/utils'
import { ScopeProvider } from '../../src/index'
import { clickButton, getTextContents } from '../utils'
import { ScopeProvider } from 'src/ScopeProvider2/ScopeProvider'
import { clickButton, getTextContents } from './utils'

describe('AtomFamily with ScopeProvider', () => {
/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useState } from 'react'
import { render, act } from '@testing-library/react'
import { atom, useAtomValue } from 'jotai'
import { ScopeProvider } from '../../src/index'
import { clickButton } from '../utils'
import { ScopeProvider } from 'src/ScopeProvider2/ScopeProvider'
import { clickButton } from './utils'

describe('ScopeProvider', () => {
it('mounts and unmounts successfully', () => {
Expand Down
18 changes: 7 additions & 11 deletions __tests__/utils.ts → __tests__/ScopeProvider2/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fireEvent } from '@testing-library/react'
import { Store } from 'src/ScopeProvider/types'
import type { Store } from 'src/ScopeProvider3/types'

function getElements(container: HTMLElement, querySelectors: string[]): Element[] {
return querySelectors.map((querySelector) => {
Expand All @@ -23,17 +23,10 @@ export function clickButton(container: HTMLElement, querySelector: string) {
fireEvent.click(button)
}

type PrdStore = Exclude<Store, { dev4_get_internal_weak_map: any }>
type DevStoreRev4 = Omit<Extract<Store, { dev4_get_internal_weak_map: any }>, keyof PrdStore>
export type PrdStore = Exclude<Store, { dev4_get_internal_weak_map: any }>
export type DevStoreRev4 = Omit<Extract<Store, { dev4_get_internal_weak_map: any }>, keyof PrdStore>

export function getDevStore(store: Store): PrdStore & DevStoreRev4 {
if (!isDevStore(store)) {
throw new Error('Store is not a dev store')
}
return store
}

export function isDevStore(store: Store): store is PrdStore & DevStoreRev4 {
function isDevStore(store: Store): store is PrdStore & DevStoreRev4 {
return (
'dev4_get_internal_weak_map' in store &&
'dev4_get_mounted_atoms' in store &&
Expand All @@ -50,3 +43,6 @@ export function assertIsDevStore(store: Store): asserts store is PrdStore & DevS
export function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms))
}

export type WithJestMock<T extends (...args: any[]) => any> = T &
jest.Mock<ReturnType<T>, Parameters<T>>
Loading
Loading