-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correct detecting context nodes (fixes #277)
- Loading branch information
Showing
3 changed files
with
106 additions
and
48 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,5 @@ | ||
--- | ||
'@solid-devtools/debugger': patch | ||
--- | ||
|
||
Correct detecting context nodes after solid 1.8 |
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 |
---|---|---|
@@ -1,56 +1,104 @@ | ||
import { | ||
createComponent, | ||
createComputed, | ||
createEffect, | ||
createMemo, | ||
createRenderEffect, | ||
createRoot, | ||
} from 'solid-js' | ||
import {describe, expect, it} from 'vitest' | ||
import * as s from 'solid-js' | ||
import * as vi from 'vitest' | ||
import {NodeType} from '../constants' | ||
import solidApi from '../solid-api' | ||
import api from '../solid-api' | ||
import {Solid} from '../types' | ||
import {getOwnerType} from '../utils' | ||
|
||
const {getOwner} = solidApi | ||
|
||
describe('getOwnerType', () => { | ||
it('identifies Component', () => { | ||
let owner!: Solid.Owner | ||
createRoot(dispose => { | ||
createComponent(() => { | ||
owner = getOwner()! | ||
vi.describe('getOwnerType', () => { | ||
const tests = { | ||
Component: () => { | ||
let owner!: Solid.Owner | ||
s.createComponent(() => { | ||
owner = api.getOwner()! | ||
return '' | ||
}, {}) | ||
dispose() | ||
}) | ||
expect(getOwnerType(owner)).toBe(NodeType.Component) | ||
}) | ||
it('identifies Effect', () => | ||
createRoot(dispose => { | ||
createEffect(() => { | ||
expect(getOwnerType(getOwner()!)).toBe(NodeType.Effect) | ||
vi.expect(getOwnerType(owner)).toBe(NodeType.Component) | ||
}, | ||
Effect: () => { | ||
s.createEffect(() => { | ||
vi.expect(getOwnerType(api.getOwner()!)).toBe(NodeType.Effect) | ||
}) | ||
}, | ||
Memo: () => { | ||
s.createMemo(() => vi.expect(getOwnerType(api.getOwner()!)).toBe(NodeType.Memo)) | ||
}, | ||
Computation: () => { | ||
s.createComputed(() => | ||
vi.expect(getOwnerType(api.getOwner()!)).toBe(NodeType.Computation), | ||
) | ||
}, | ||
Render: () => { | ||
s.createRenderEffect(() => | ||
vi.expect(getOwnerType(api.getOwner()!)).toBe(NodeType.Render), | ||
) | ||
}, | ||
Root: () => { | ||
s.createRoot(dispose => { | ||
vi.expect(getOwnerType(api.getOwner()!)).toBe(NodeType.Root) | ||
dispose() | ||
}) | ||
})) | ||
it('identifies Memo', () => | ||
createRoot(dispose => { | ||
createMemo(() => expect(getOwnerType(getOwner()!)).toBe(NodeType.Memo)) | ||
dispose() | ||
})) | ||
it('identifies Computation', () => | ||
createRoot(dispose => { | ||
createComputed(() => expect(getOwnerType(getOwner()!)).toBe(NodeType.Computation)) | ||
dispose() | ||
})) | ||
it('identifies Render Effect', () => | ||
createRoot(dispose => { | ||
createRenderEffect(() => expect(getOwnerType(getOwner()!)).toBe(NodeType.Render)) | ||
dispose() | ||
})) | ||
it('identifies Root', () => | ||
createRoot(dispose => { | ||
expect(getOwnerType(getOwner()!)).toBe(NodeType.Root) | ||
dispose() | ||
})) | ||
}, | ||
Context: () => { | ||
let memo!: Solid.Owner | ||
const Ctx = s.createContext(0) | ||
Ctx.Provider({ | ||
value: 1, | ||
get children() { | ||
memo = api.getOwner()! | ||
return '' | ||
}, | ||
}) | ||
const ctx = memo.owner! | ||
vi.expect(getOwnerType(ctx)).toBe(NodeType.Context) | ||
vi.expect(ctx.owned).toHaveLength(2) | ||
vi.expect(getOwnerType(ctx.owned![0]!)).toBe(NodeType.Memo) | ||
vi.expect(getOwnerType(ctx.owned![1]!)).toBe(NodeType.Memo) | ||
vi.expect(ctx.owned![0]!).toBe(memo) | ||
}, | ||
} | ||
|
||
const apis: Record<string, (cb: () => void) => void> = { | ||
root: cb => { | ||
let dispose!: () => void | ||
s.createRoot(d => { | ||
dispose = d | ||
cb() | ||
}) | ||
api.onCleanup(dispose) | ||
}, | ||
memo: s.createMemo, | ||
effect: s.createEffect, | ||
render: s.createRenderEffect, | ||
computed: s.createComputed, | ||
component: cb => { | ||
s.createComponent(() => { | ||
cb() | ||
return '' | ||
}, {}) | ||
}, | ||
context: cb => { | ||
const Ctx = s.createContext(0) | ||
Ctx.Provider({ | ||
value: 1, | ||
get children() { | ||
cb() | ||
return '' | ||
}, | ||
}) | ||
}, | ||
} | ||
|
||
for (const [wrapper_name, wrapper] of Object.entries(apis)) { | ||
vi.describe('under ' + wrapper_name, () => { | ||
for (const [name, test] of Object.entries(tests)) { | ||
vi.test('identifies ' + name, () => { | ||
s.createRoot(d => { | ||
wrapper(test) | ||
d() | ||
}) | ||
}) | ||
} | ||
}) | ||
} | ||
}) |
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