1
+ import { createStubFileSystem } from "../adapters/fileSystem.stub" ;
1
2
import {
2
3
findEditorConfiguration ,
3
4
FindEditorConfigurationDependencies ,
4
5
} from "./findEditorConfiguration" ;
6
+ import { DEFAULT_VSCODE_SETTINGS_PATH } from "./vsCodeSettings" ;
5
7
6
8
const stubConfigPath = "temp/" ;
7
9
@@ -10,10 +12,46 @@ export const createStubImporter = (filePath = "") =>
10
12
11
13
const createStubDependencies = ( overrides : Partial < FindEditorConfigurationDependencies > = { } ) => ( {
12
14
importer : createStubImporter ( stubConfigPath ) ,
15
+ fileSystem : createStubFileSystem ( ) ,
13
16
...overrides ,
14
17
} ) ;
15
18
16
19
describe ( "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
+
17
55
it ( "returns an error when importer returns one" , async ( ) => {
18
56
// Arrange
19
57
const message = "error" ;
@@ -27,11 +65,12 @@ describe("findEditorConfiguration", () => {
27
65
const result = await findEditorConfiguration ( dependencies , stubConfigPath ) ;
28
66
29
67
// Assert
30
- expect ( result ) . toEqual (
31
- expect . objectContaining ( {
68
+ expect ( result ) . toEqual ( {
69
+ configPath : stubConfigPath ,
70
+ result : expect . objectContaining ( {
32
71
message,
33
72
} ) ,
34
- ) ;
73
+ } ) ;
35
74
} ) ;
36
75
37
76
it ( "reads from the given configuration path when one is provided" , async ( ) => {
@@ -46,25 +85,30 @@ describe("findEditorConfiguration", () => {
46
85
expect ( dependencies . importer ) . toHaveBeenLastCalledWith ( configPath ) ;
47
86
} ) ;
48
87
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 ( ) => {
50
89
// 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
+ } ) ;
52
97
53
98
// Act
54
- await findEditorConfiguration ( dependencies , undefined ) ;
99
+ const result = await findEditorConfiguration ( dependencies , undefined ) ;
55
100
56
101
// Assert
57
- expect ( dependencies . importer ) . toHaveBeenLastCalledWith ( ".vscode/settings.json" ) ;
102
+ expect ( result ) . toEqual ( {
103
+ configPath : DEFAULT_VSCODE_SETTINGS_PATH ,
104
+ result : originalConfig ,
105
+ } ) ;
58
106
} ) ;
59
107
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 ( ) => {
61
109
// Arrange
62
110
const originalConfig = {
63
111
"typescript.tsdk" : "node_modules/typescript/lib" ,
64
- "editor.tabSize" : 4 ,
65
- "editor.codeActionsOnSave" : {
66
- "source.organizeImports" : false ,
67
- } ,
68
112
} ;
69
113
70
114
const dependencies = createStubDependencies ( {
@@ -75,6 +119,9 @@ describe("findEditorConfiguration", () => {
75
119
const result = await findEditorConfiguration ( dependencies , stubConfigPath ) ;
76
120
77
121
// Assert
78
- expect ( result ) . toEqual ( originalConfig ) ;
122
+ expect ( result ) . toEqual ( {
123
+ configPath : stubConfigPath ,
124
+ result : originalConfig ,
125
+ } ) ;
79
126
} ) ;
80
127
} ) ;
0 commit comments