@@ -2,12 +2,22 @@ import type { Nuxt } from 'nuxt/schema'
22import type { NuxtDevtoolsServerContext } from '../src/types'
33import fsp from 'node:fs/promises'
44import { tmpdir } from 'node:os'
5+ import { launchEditor } from 'devframe/utils/launch-editor'
56import { createHooks } from 'hookable'
67import { join , normalize } from 'pathe'
78import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest'
89import { setupGeneralRPC } from '../src/server-rpc/general'
10+ import { getOptions } from '../src/server-rpc/options'
911
10- function fakeContext ( root : string ) {
12+ vi . mock ( 'devframe/utils/launch-editor' , ( ) => ( {
13+ launchEditor : vi . fn ( ) ,
14+ } ) )
15+
16+ vi . mock ( '../src/server-rpc/options' , ( ) => ( {
17+ getOptions : vi . fn ( ) ,
18+ } ) )
19+
20+ function fakeContext ( root : string , handledByHook = true ) {
1121 const hooks = createHooks ( )
1222 const opened : string [ ] = [ ]
1323 const ctx = {
@@ -23,7 +33,7 @@ function fakeContext(root: string) {
2333 refresh : ( ) => { } ,
2434 openInEditorHooks : [ ( filepath : string ) => {
2535 opened . push ( filepath )
26- return true
36+ return handledByHook
2737 } ] ,
2838 } as unknown as NuxtDevtoolsServerContext
2939 return { ctx, opened }
@@ -37,6 +47,8 @@ describe('openInEditor', () => {
3747 await fsp . mkdir ( join ( root , 'apps/web/app/components' ) , { recursive : true } )
3848 await fsp . writeFile ( join ( root , 'apps/web/app/components/Foo.vue' ) , '<template><div /></template>' )
3949 vi . spyOn ( process , 'cwd' ) . mockReturnValue ( join ( root , 'apps/web' ) )
50+ vi . mocked ( getOptions ) . mockReset ( )
51+ vi . mocked ( launchEditor ) . mockClear ( )
4052 } )
4153
4254 afterEach ( async ( ) => {
@@ -78,6 +90,19 @@ describe('openInEditor', () => {
7890 expect ( opened ) . toEqual ( [ filepath ] )
7991 } )
8092
93+ it . each ( [ 'subl' , 'sublime' ] ) ( 'opens Sublime Text with subl for the %s setting' , async ( setting ) => {
94+ vi . mocked ( getOptions ) . mockReturnValue ( {
95+ behavior : { openInEditor : setting } ,
96+ } as any )
97+ const { ctx, opened } = fakeContext ( root , false )
98+ const { openInEditor } = setupGeneralRPC ( ctx )
99+ const filepath = join ( root , 'apps/web/app/components/Foo.vue' )
100+
101+ await expect ( openInEditor ( 'app/components/Foo.vue' ) ) . resolves . toBe ( true )
102+ expect ( opened ) . toEqual ( [ filepath ] )
103+ expect ( launchEditor ) . toHaveBeenCalledWith ( filepath , 'subl' )
104+ } )
105+
81106 it ( 'reports a path that exists under none of the base directories' , async ( ) => {
82107 const error = vi . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
83108 const { ctx, opened } = fakeContext ( root )
0 commit comments