Skip to content

Commit

Permalink
Move tests together with source files
Browse files Browse the repository at this point in the history
  • Loading branch information
thetarnav committed Jan 4, 2025
1 parent d229454 commit af7b30f
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 101 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import '../../setup.ts'
import '../setup.ts'

import * as s from 'solid-js'
import * as test from 'vitest'
import {NodeType} from '../../main/constants.ts'
import {ObjectType, getSdtId} from '../../main/id.ts'
import setup from '../../main/setup.ts'
import type {NodeID, Solid} from '../../main/types.ts'
import {type SerializedDGraph, collectDependencyGraph} from '../collect.ts'
import {NodeType} from '../main/constants.ts'
import {ObjectType, getSdtId} from '../main/id.ts'
import setup from '../main/setup.ts'
import type {NodeID, Solid} from '../main/types.ts'
import {type SerializedDGraph, collectDependencyGraph} from './collect.ts'

let mockLAST_ID = 0
test.beforeEach(() => {
mockLAST_ID = 0
})
test.vi.mock('../../main/get-id', () => ({getNewSdtId: () => '#' + mockLAST_ID++}))
test.vi.mock('../main/get-id.ts', () => ({getNewSdtId: () => '#' + mockLAST_ID++}))

test.describe('collectDependencyGraph', () => {
test.it('should collect dependency graph', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import '../../setup.ts'
import '../setup.ts'

import * as s from 'solid-js'
import {beforeEach, describe, expect, it, vi} from 'vitest'
import {getObjectById, getSdtId, ObjectType} from '../../main/id.ts'
import setup from '../../main/setup.ts'
import {type Mapped, NodeType, PropGetterState, type Solid, ValueType} from '../../types.ts'
import {collectOwnerDetails} from '../inspector.ts'

let mockLAST_ID = 0
beforeEach(() => {
mockLAST_ID = 0
})
vi.mock('../../main/get-id', () => ({getNewSdtId: () => '#' + mockLAST_ID++}))

describe('collectOwnerDetails', () => {
it('collects focused owner details', () => {
import * as test from 'vitest'
import {getObjectById, getSdtId, ObjectType} from '../main/id.ts'
import setup from '../main/setup.ts'
import {type Mapped, NodeType, PropGetterState, type Solid, ValueType} from '../types.ts'
import {collectOwnerDetails} from './inspector.ts'

test.describe('collectOwnerDetails', () => {
test.it('collects focused owner details', () => {
s.createRoot(dispose => {
const [source] = s.createSignal(0, {name: 'source'})

Expand Down Expand Up @@ -57,7 +51,7 @@ describe('collectOwnerDetails', () => {
},
})

expect(details).toEqual({
test.expect(details).toEqual({
id: getSdtId(memo, ObjectType.Owner),
name: 'focused',
type: NodeType.Memo,
Expand All @@ -84,26 +78,29 @@ describe('collectOwnerDetails', () => {
],
} satisfies Mapped.OwnerDetails)

expect(valueMap.get(`signal:${getSdtId(customValue, ObjectType.CustomValue)}`)).toBeTruthy()
expect(valueMap.get(`signal:${getSdtId(signalB, ObjectType.Signal)}`)).toBeTruthy()
expect(valueMap.get(`signal:${getSdtId(innerMemo, ObjectType.Owner)}`)).toBeTruthy()
test.expect(valueMap.get(`signal:${getSdtId(customValue, ObjectType.CustomValue)}`)).toBeTruthy()
test.expect(valueMap.get(`signal:${getSdtId(signalB, ObjectType.Signal)}`)).toBeTruthy()
test.expect(valueMap.get(`signal:${getSdtId(innerMemo, ObjectType.Owner)}`)).toBeTruthy()

expect(getObjectById('#3', ObjectType.Element)).toBe(div)
test.expect(getObjectById('#3', ObjectType.Element)).toBe(div)

dispose()
})
})

it('component props', () => {
test.it('component props', () => {
s.createRoot(dispose => {

let owner!: Solid.Owner
let div_ref!: HTMLDivElement

const TestComponent = (props: {
count: number
children: s.JSX.Element
nested: {foo: number; bar: string}
}) => {
owner = setup.solid.getOwner()!
return <div>{props.children}</div>
return <div ref={div_ref}>{props.children}</div>
}
s.createRenderEffect(() => (
<TestComponent count={123} nested={{foo: 1, bar: '2'}}>
Expand All @@ -123,12 +120,12 @@ describe('collectOwnerDetails', () => {

dispose()

expect(details).toEqual({
id: '#0',
test.expect(details).toEqual({
id: getSdtId(owner, ObjectType.Owner),
name: 'TestComponent',
type: NodeType.Component,
signals: [],
value: [[ValueType.Element, '#1:div']],
value: [[ValueType.Element, `${getSdtId(div_ref, ObjectType.Element)}:div`]],
props: {
proxy: false,
record: {
Expand All @@ -147,14 +144,15 @@ describe('collectOwnerDetails', () => {
},
},
} satisfies Mapped.OwnerDetails)

expect(getObjectById('#1', ObjectType.Element)).toBeInstanceOf(HTMLDivElement)
})
})

it('dynamic component props', () => {
test.it('dynamic component props', () => {
s.createRoot(dispose => {

let owner!: Solid.Owner
let el_ref!: HTMLDivElement

const Button = (props: s.JSX.ButtonHTMLAttributes<HTMLButtonElement>) => {
owner = setup.solid.getOwner()!
return <button {...props}>Click me</button>
Expand All @@ -167,7 +165,7 @@ describe('collectOwnerDetails', () => {
},
role: 'button',
}) as const
return <Button {...props()} />
return (el_ref = <Button {...props()} /> as any)
})

const {details} = collectOwnerDetails(owner, {
Expand All @@ -180,12 +178,12 @@ describe('collectOwnerDetails', () => {
},
})

expect(details).toEqual({
id: '#0',
test.expect(details).toEqual({
id: getSdtId(owner, ObjectType.Owner),
name: 'Button',
type: NodeType.Component,
signals: [],
value: [[ValueType.Element, '#1:button']],
value: [[ValueType.Element, `${getSdtId(el_ref, ObjectType.Element)}:button`]],
props: {
proxy: true,
record: {
Expand All @@ -201,13 +199,11 @@ describe('collectOwnerDetails', () => {
},
} satisfies Mapped.OwnerDetails)

expect(getObjectById('#1', ObjectType.Element)).toBeInstanceOf(HTMLButtonElement)

dispose()
})
})

it('listens to value updates', () => {
test.it('listens to value updates', () => {
s.createRoot(dispose => {
let owner!: Solid.Owner

Expand All @@ -217,7 +213,7 @@ describe('collectOwnerDetails', () => {
return count()
})

const onValueUpdate = vi.fn()
const onValueUpdate = test.vi.fn()
collectOwnerDetails(owner, {
observedPropsMap: new WeakMap(),
onPropStateChange: () => {
Expand All @@ -226,30 +222,32 @@ describe('collectOwnerDetails', () => {
onValueUpdate: onValueUpdate,
})

expect(onValueUpdate).not.toBeCalled()
test.expect(onValueUpdate).not.toBeCalled()

setCount(1)
expect(onValueUpdate).toBeCalledTimes(1)
expect(onValueUpdate).toHaveBeenLastCalledWith('value')
test.expect(onValueUpdate).toBeCalledTimes(1)
test.expect(onValueUpdate).toHaveBeenLastCalledWith('value')

setCount(2)
expect(onValueUpdate).toBeCalledTimes(2)
expect(onValueUpdate).toHaveBeenLastCalledWith('value')
test.expect(onValueUpdate).toBeCalledTimes(2)
test.expect(onValueUpdate).toHaveBeenLastCalledWith('value')

setCount(2)
expect(onValueUpdate).toBeCalledTimes(2)
test.expect(onValueUpdate).toBeCalledTimes(2)

dispose()
})
})

it('listens to signal updates', () => {
test.it('listens to signal updates', () => {
s.createRoot(dispose => {
const owner = setup.solid.getOwner()!
const [, setCount] = s.createSignal(0) // id: "0"
const [, setCount2] = s.createSignal(0) // id: "1"
const [, setCount1] = s.createSignal(0)
const [, setCount2] = s.createSignal(0)

const [count1, count2] = owner.sourceMap as [Solid.Signal, Solid.Signal]

const onValueUpdate = vi.fn()
const onValueUpdate = test.vi.fn()
collectOwnerDetails(owner, {
observedPropsMap: new WeakMap(),
onPropStateChange: () => {
Expand All @@ -258,18 +256,18 @@ describe('collectOwnerDetails', () => {
onValueUpdate: onValueUpdate,
})

expect(onValueUpdate).not.toBeCalled()
test.expect(onValueUpdate).not.toBeCalled()

setCount(1)
expect(onValueUpdate).toBeCalledTimes(1)
expect(onValueUpdate).toHaveBeenLastCalledWith('signal:#1')
setCount1(1)
test.expect(onValueUpdate).toBeCalledTimes(1)
test.expect(onValueUpdate).toHaveBeenLastCalledWith(`signal:${getSdtId(count1, ObjectType.Signal)}`)

setCount(1)
expect(onValueUpdate).toBeCalledTimes(1)
setCount1(1)
test.expect(onValueUpdate).toBeCalledTimes(1)

setCount2(1)
expect(onValueUpdate).toBeCalledTimes(2)
expect(onValueUpdate).toHaveBeenLastCalledWith('signal:#2')
test.expect(onValueUpdate).toBeCalledTimes(2)
test.expect(onValueUpdate).toHaveBeenLastCalledWith(`signal:${getSdtId(count2, ObjectType.Signal)}`)

dispose()
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import '../../setup.ts'
import '../setup.ts'

import {type Truthy} from '@solid-primitives/utils'
import {createMutable, createStore} from 'solid-js/store'
import {describe, expect, test, vi} from 'vitest'
import {ObjectType, getObjectById} from '../../main/id.ts'
import {encodeValue} from '../serialize.ts'
import {type EncodedValue, INFINITY, NAN, NEGATIVE_INFINITY, UNDEFINED, ValueType} from '../types.ts'
import {ObjectType, getObjectById} from '../main/id.ts'
import {encodeValue} from './serialize.ts'
import {type EncodedValue, INFINITY, NAN, NEGATIVE_INFINITY, UNDEFINED, ValueType} from './types.ts'

let mockLAST_ID = 0
vi.mock('../../main/get-id', () => ({getNewSdtId: () => '#' + mockLAST_ID++}))
vi.mock('../main/get-id.ts', () => ({getNewSdtId: () => '#' + mockLAST_ID++}))

type Expectations = [name: string, data: unknown, encoded: EncodedValue[]][]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import '../../setup.ts'
import '../setup.ts'

import {createRoot} from 'solid-js'
import {createMutable, createStore, modifyMutable, produce, reconcile, unwrap} from 'solid-js/store'
import {beforeEach, describe, expect, it, vi} from 'vitest'
import {ObjectType, getSdtId} from '../../main/id.ts'
import setup from '../../main/setup.ts'
import {isSolidStore} from '../../main/utils.ts'
import {type Solid} from '../../types.ts'
import {type OnNodeUpdate, type StoreNodeProperty, observeStoreNode, setOnStoreNodeUpdate} from '../store.ts'
import {ObjectType, getSdtId} from '../main/id.ts'
import setup from '../main/setup.ts'
import {isSolidStore} from '../main/utils.ts'
import {type Solid} from '../types.ts'
import {type OnNodeUpdate, type StoreNodeProperty, observeStoreNode, setOnStoreNodeUpdate} from './store.ts'

const getOwnerStore = () => {
const owner = setup.solid.getOwner()
Expand All @@ -25,7 +25,7 @@ let mockLAST_ID = 0
beforeEach(() => {
mockLAST_ID = 0
})
vi.mock('../../main/get-id', () => ({getNewSdtId: () => '#' + mockLAST_ID++}))
vi.mock('../main/get-id.ts', () => ({getNewSdtId: () => '#' + mockLAST_ID++}))

type UpdateParams = Parameters<OnNodeUpdate>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ vi.mock('@solid-primitives/platform', () => ({
},
}))

const fetchFunction = async () => (await import('../find-components.ts')).parseLocationString
const fetchFunction = async () => (await import('./find-components.ts')).parseLocationString

describe('locator attribute pasting', () => {
beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import '../../setup.ts'
import '../setup.ts'

import {createComputed, createRoot, createSignal, onCleanup} from 'solid-js'
import {describe, expect, it} from 'vitest'
Expand All @@ -7,8 +7,8 @@ import {
interceptComputationRerun,
observeValueUpdate,
removeValueUpdateObserver,
} from '../observe.ts'
import setup from '../setup.ts'
} from './observe.ts'
import setup from './setup.ts'

describe('addSolidUpdateListener', () => {
it('listens to solid updates', () =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import '../../setup.ts'
import '../setup.ts'

import * as s from 'solid-js'
import * as vi from 'vitest'
import {NodeType} from '../constants.ts'
import {NodeType} from './constants.ts'
import {type Solid} from '../types.ts'
import * as utils from '../utils.ts'
import setup from '../setup.ts'
import * as utils from './utils.ts'
import setup from './setup.ts'

vi.describe('getOwnerType', () => {
const tests = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import '../../setup.ts'
import '../setup.ts'

import * as s from 'solid-js'
import * as test from 'vitest'
import {NodeType, TreeWalkerMode} from '../../main/constants.ts'
import {$setSdtId} from '../../main/id.ts'
import {type Mapped, type Solid} from '../../main/types.ts'
import {getNodeName} from '../../main/utils.ts'
import {type ComputationUpdateHandler, walkSolidTree} from '../walker.ts'
import setup from '../../main/setup.ts'
import {initRoots} from '../../main/roots.ts'
import {NodeType, TreeWalkerMode} from '../main/constants.ts'
import {$setSdtId} from '../main/id.ts'
import {type Mapped, type Solid} from '../main/types.ts'
import {getNodeName} from '../main/utils.ts'
import {type ComputationUpdateHandler, walkSolidTree} from './walker.ts'
import setup from '../main/setup.ts'
import {initRoots} from '../main/roots.ts'

test.beforeAll(() => {
initRoots()
Expand All @@ -18,7 +18,7 @@ let mockLAST_ID = 0
test.beforeEach(() => {
mockLAST_ID = 0
})
test.vi.mock('../../main/get-id', () => ({getNewSdtId: () => '#' + mockLAST_ID++}))
test.vi.mock('../main/get-id.ts', () => ({getNewSdtId: () => '#' + mockLAST_ID++}))

test.describe('TreeWalkerMode.Owners', () => {
test.it('default options', () => {
Expand Down
11 changes: 0 additions & 11 deletions packages/debugger/test_setup.ts

This file was deleted.

0 comments on commit af7b30f

Please sign in to comment.