Skip to content
Source
Sign inSign up
TanStack/devtools
Open History to see the last commit

editor.test.ts

75 lines2.4 KB

1import { describe, expect, it, vi } from 'vitest'
2import { handleOpenSource } from './editor'
3​
4describe('handleOpenSource', () => {
5 it('calls openInEditor with source, stringified line and column when all provided', async () => {
6 const openInEditor = vi.fn().mockResolvedValue(undefined)
7 await handleOpenSource({
8 data: {
9 type: 'open-source',
10 data: { source: '/app/routes/foo.tsx', line: 10, column: 5 },
11 },
12 openInEditor,
13 })
14 expect(openInEditor).toHaveBeenCalledOnce()
15 expect(openInEditor).toHaveBeenCalledWith('/app/routes/foo.tsx', '10', '5')
16 })
17​
18 it('calls openInEditor with source only when line and column are undefined', async () => {
19 const openInEditor = vi.fn().mockResolvedValue(undefined)
20 await handleOpenSource({
21 data: { type: 'open-source', data: { source: '/app/routes/bar.tsx' } },
22 openInEditor,
23 })
24 expect(openInEditor).toHaveBeenCalledOnce()
25 expect(openInEditor).toHaveBeenCalledWith(
26 '/app/routes/bar.tsx',
27 undefined,
28 undefined,
29 )
30 })
31​
32 it('does NOT call openInEditor and returns undefined when source is absent', async () => {
33 const openInEditor = vi.fn()
34 const result = await handleOpenSource({
35 data: { type: 'open-source', data: { line: 1, column: 2 } },
36 openInEditor,
37 })
38 expect(openInEditor).not.toHaveBeenCalled()
39 expect(result).toBeUndefined()
40 })
41​
42 it('treats falsy line: 0 and column: 0 as undefined (truthy check in impl)', async () => {
43 const openInEditor = vi.fn().mockResolvedValue(undefined)
44 await handleOpenSource({
45 data: {
46 type: 'open-source',
47 data: { source: '/app/routes/baz.tsx', line: 0, column: 0 },
48 },
49 openInEditor,
50 })
51 expect(openInEditor).toHaveBeenCalledOnce()
52 expect(openInEditor).toHaveBeenCalledWith(
53 '/app/routes/baz.tsx',
54 undefined,
55 undefined,
56 )
57 })
58​
59 it('calls openInEditor with stringified line and undefined column when only line is provided', async () => {
60 const openInEditor = vi.fn().mockResolvedValue(undefined)
61 await handleOpenSource({
62 data: {
63 type: 'open-source',
64 data: { source: '/app/routes/qux.tsx', line: 42 },
65 },
66 openInEditor,
67 })
68 expect(openInEditor).toHaveBeenCalledOnce()
69 expect(openInEditor).toHaveBeenCalledWith(
70 '/app/routes/qux.tsx',
71 '42',
72 undefined,
73 )
74 })
75})