1+ import { createStubFileSystem } from "../adapters/fileSystem.stub" ;
12import {
23 findEditorConfiguration ,
34 FindEditorConfigurationDependencies ,
45} from "./findEditorConfiguration" ;
6+ import { DEFAULT_VSCODE_SETTINGS_PATH } from "./vsCodeSettings" ;
57
68const stubConfigPath = "temp/" ;
79
@@ -10,10 +12,46 @@ export const createStubImporter = (filePath = "") =>
1012
1113const createStubDependencies = ( overrides : Partial < FindEditorConfigurationDependencies > = { } ) => ( {
1214 importer : createStubImporter ( stubConfigPath ) ,
15+ fileSystem : createStubFileSystem ( ) ,
1316 ...overrides ,
1417} ) ;
1518
1619describe ( "findEditorConfiguration" , ( ) => {
20+ it ( "returns undefined when the file is not specified and does not exist" , async ( ) => {
21+ // Arrange
22+ const dependencies = createStubDependencies ( {
23+ fileSystem : {
24+ fileExists : async ( ) => false ,
25+ } ,
26+ } ) ;
27+
28+ // Act
29+ const result = await findEditorConfiguration ( dependencies , undefined ) ;
30+
31+ // Assert
32+ expect ( result ) . toEqual ( undefined ) ;
33+ } ) ;
34+
35+ it ( "returns an error when the file is specified and does not exist" , async ( ) => {
36+ // Arrange
37+ const dependencies = createStubDependencies ( {
38+ fileSystem : {
39+ fileExists : async ( ) => false ,
40+ } ,
41+ } ) ;
42+
43+ // Act
44+ const result = await findEditorConfiguration ( dependencies , stubConfigPath ) ;
45+
46+ // Assert
47+ expect ( result ) . toEqual ( {
48+ configPath : stubConfigPath ,
49+ result : expect . objectContaining ( {
50+ message : `Could not find editor configuration under '${ stubConfigPath } '.` ,
51+ } ) ,
52+ } ) ;
53+ } ) ;
54+
1755 it ( "returns an error when importer returns one" , async ( ) => {
1856 // Arrange
1957 const message = "error" ;
@@ -27,11 +65,12 @@ describe("findEditorConfiguration", () => {
2765 const result = await findEditorConfiguration ( dependencies , stubConfigPath ) ;
2866
2967 // Assert
30- expect ( result ) . toEqual (
31- expect . objectContaining ( {
68+ expect ( result ) . toEqual ( {
69+ configPath : stubConfigPath ,
70+ result : expect . objectContaining ( {
3271 message,
3372 } ) ,
34- ) ;
73+ } ) ;
3574 } ) ;
3675
3776 it ( "reads from the given configuration path when one is provided" , async ( ) => {
@@ -46,25 +85,30 @@ describe("findEditorConfiguration", () => {
4685 expect ( dependencies . importer ) . toHaveBeenLastCalledWith ( configPath ) ;
4786 } ) ;
4887
49- it ( "defaults to VS Code editor settings path when config path isn't provided " , async ( ) => {
88+ it ( "parses object from the default VS Code configuration path when the file is not specified and read successfully " , async ( ) => {
5089 // Arrange
51- const dependencies = createStubDependencies ( ) ;
90+ const originalConfig = {
91+ "typescript.tsdk" : "node_modules/typescript/lib" ,
92+ } ;
93+
94+ const dependencies = createStubDependencies ( {
95+ importer : async ( ) => originalConfig ,
96+ } ) ;
5297
5398 // Act
54- await findEditorConfiguration ( dependencies , undefined ) ;
99+ const result = await findEditorConfiguration ( dependencies , undefined ) ;
55100
56101 // Assert
57- expect ( dependencies . importer ) . toHaveBeenLastCalledWith ( ".vscode/settings.json" ) ;
102+ expect ( result ) . toEqual ( {
103+ configPath : DEFAULT_VSCODE_SETTINGS_PATH ,
104+ result : originalConfig ,
105+ } ) ;
58106 } ) ;
59107
60- it ( "parses object from configuration path when read successfully" , async ( ) => {
108+ it ( "parses object from configuration path when the file is specified and read successfully" , async ( ) => {
61109 // Arrange
62110 const originalConfig = {
63111 "typescript.tsdk" : "node_modules/typescript/lib" ,
64- "editor.tabSize" : 4 ,
65- "editor.codeActionsOnSave" : {
66- "source.organizeImports" : false ,
67- } ,
68112 } ;
69113
70114 const dependencies = createStubDependencies ( {
@@ -75,6 +119,9 @@ describe("findEditorConfiguration", () => {
75119 const result = await findEditorConfiguration ( dependencies , stubConfigPath ) ;
76120
77121 // Assert
78- expect ( result ) . toEqual ( originalConfig ) ;
122+ expect ( result ) . toEqual ( {
123+ configPath : stubConfigPath ,
124+ result : originalConfig ,
125+ } ) ;
79126 } ) ;
80127} ) ;
0 commit comments