-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sort projects in project view (#1137)
- Loading branch information
1 parent
fc4caea
commit 7cc9ebe
Showing
6 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const cacheJson = jest.fn(); | ||
|
||
const fileExistsSync = jest.fn(); | ||
|
||
const getOutputChannel = jest.fn(() => ({ | ||
appendLine: jest.fn(), | ||
show: jest.fn(), | ||
})); | ||
|
||
const getTelemetry = jest.fn(() => ({ | ||
exception: jest.fn(), | ||
})); | ||
|
||
const toWorkspaceFormat = jest.fn(); | ||
|
||
export { | ||
cacheJson, | ||
fileExistsSync, | ||
getOutputChannel, | ||
getTelemetry, | ||
toWorkspaceFormat, | ||
}; |
7 changes: 7 additions & 0 deletions
7
libs/vscode/nx-workspace/__mocks__/@nx-console/vscode/configuration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const WorkspaceConfigurationStore = { | ||
instance: { | ||
get: jest.fn(), | ||
}, | ||
}; | ||
|
||
export { WorkspaceConfigurationStore }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/// <reference types="jest" /> | ||
|
||
const window = { | ||
showErrorMessage: jest.fn(), | ||
}; | ||
|
||
export = { | ||
window, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
178 changes: 178 additions & 0 deletions
178
libs/vscode/nx-workspace/src/lib/verify-workspace.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
import { verifyWorkspace } from './verify-workspace'; | ||
import { WorkspaceConfigurationStore } from '@nx-console/vscode/configuration'; | ||
import * as server from '@nx-console/server'; | ||
import { | ||
cacheJson, | ||
fileExistsSync, | ||
getOutputChannel, | ||
getTelemetry, | ||
} from '@nx-console/server'; | ||
import type { WorkspaceJsonConfiguration } from '@nrwl/devkit'; | ||
import * as vscode from 'vscode'; | ||
|
||
const mockCacheJsonFn = cacheJson as jest.MockedFunction<typeof cacheJson>; | ||
mockCacheJsonFn.mockImplementation((filePath) => ({ | ||
json: mockWorkspace, | ||
path: filePath, | ||
})); | ||
|
||
const mockStoreInstanceGetFn = WorkspaceConfigurationStore.instance | ||
.get as jest.MockedFunction<typeof WorkspaceConfigurationStore.instance.get>; | ||
mockStoreInstanceGetFn.mockImplementation(() => workspacePath); | ||
|
||
const mockFileExistsSyncFn = fileExistsSync as jest.MockedFunction< | ||
typeof fileExistsSync | ||
>; | ||
|
||
const originalNxConsoleServerModule = jest.requireActual('@nx-console/server'); | ||
(server.toWorkspaceFormat as unknown) = | ||
originalNxConsoleServerModule.toWorkspaceFormat; | ||
|
||
const mockWorkspace: WorkspaceJsonConfiguration = { | ||
version: 2, | ||
projects: { | ||
Project3: { | ||
root: 'project-three', | ||
}, | ||
Project1: { | ||
root: 'project-one', | ||
}, | ||
Project2: { | ||
root: 'project-two', | ||
}, | ||
}, | ||
defaultProject: undefined, | ||
generators: undefined, | ||
cli: undefined, | ||
}; | ||
|
||
const DefaultWorkspaceInformation = { | ||
validWorkspaceJson: false, | ||
workspaceType: 'nx', | ||
json: { | ||
projects: {}, | ||
version: 2, | ||
}, | ||
configurationFilePath: '', | ||
}; | ||
|
||
const workspacePath = './test/fixtures/workspace/'; | ||
|
||
describe(verifyWorkspace.name, () => { | ||
afterEach(() => { | ||
mockFileExistsSyncFn.mockClear(); | ||
}); | ||
|
||
describe('when Nx workspace exists', () => { | ||
it('returns information about Nx workspace', async () => { | ||
// arrange | ||
mockFileExistsSyncFn.mockImplementation((filePath) => | ||
/workspace.json$/i.test(filePath) | ||
); | ||
|
||
// act | ||
const { validWorkspaceJson, json, workspaceType, configurationFilePath } = | ||
await verifyWorkspace(); | ||
|
||
// assert | ||
expect(mockFileExistsSyncFn).toHaveBeenCalledTimes(1); | ||
expect(mockFileExistsSyncFn).toHaveLastReturnedWith(true); | ||
expect(mockStoreInstanceGetFn).toHaveBeenCalledWith( | ||
'nxWorkspaceJsonPath', | ||
'' | ||
); | ||
expect(mockCacheJsonFn).toHaveBeenCalled(); | ||
expect(validWorkspaceJson).toBe(true); | ||
expect(json).toBeTruthy(); | ||
expect(json).toEqual(mockWorkspace); | ||
expect(workspaceType).toBe('nx'); | ||
expect(configurationFilePath).toMatch(/workspace.json$/i); | ||
}); | ||
}); | ||
|
||
describe('when Ng workspace exists', () => { | ||
it('returns information about Ng workspace', async () => { | ||
// arrange | ||
mockFileExistsSyncFn.mockImplementation((filePath) => | ||
/angular.json$/i.test(filePath) | ||
); | ||
|
||
// act | ||
const { validWorkspaceJson, json, workspaceType, configurationFilePath } = | ||
await verifyWorkspace(); | ||
|
||
// assert | ||
expect(mockFileExistsSyncFn).toHaveBeenCalledTimes(2); | ||
expect(mockFileExistsSyncFn).toHaveNthReturnedWith(1, false); | ||
expect(mockFileExistsSyncFn).toHaveNthReturnedWith(2, true); | ||
expect(mockStoreInstanceGetFn).toHaveBeenCalledWith( | ||
'nxWorkspaceJsonPath', | ||
'' | ||
); | ||
expect(mockCacheJsonFn).toHaveBeenCalled(); | ||
expect(validWorkspaceJson).toBe(true); | ||
expect(json).toBeTruthy(); | ||
expect(json).toEqual(mockWorkspace); | ||
expect(workspaceType).toBe('ng'); | ||
expect(configurationFilePath).toMatch(/angular.json$/i); | ||
}); | ||
}); | ||
|
||
describe('when workspace json does not exist', () => { | ||
it('it shows error dialog and returns default workspace information', async () => { | ||
// arrange | ||
mockFileExistsSyncFn.mockImplementation( | ||
(filePath) => !/(angular|workspace).json$/i.test(filePath) | ||
); | ||
|
||
(vscode.window.showErrorMessage as unknown) = jest | ||
.fn() | ||
.mockResolvedValue('Show Error'); | ||
|
||
// act | ||
const result = await verifyWorkspace(); | ||
|
||
// assert | ||
expect(mockFileExistsSyncFn).toHaveBeenCalledTimes(2); | ||
expect(mockFileExistsSyncFn).toHaveNthReturnedWith(1, false); | ||
expect(mockFileExistsSyncFn).toHaveNthReturnedWith(2, false); | ||
expect(vscode.window.showErrorMessage).toHaveBeenCalledWith( | ||
expect.any(String), | ||
'Show Error' | ||
); | ||
expect(getOutputChannel).toHaveBeenCalledTimes(3); | ||
expect(getTelemetry).toHaveBeenCalledTimes(1); | ||
expect(result).toEqual(DefaultWorkspaceInformation); | ||
}); | ||
}); | ||
|
||
describe('when workspace information is found', () => { | ||
it('projects entries are sorted by entry key', async () => { | ||
// arrange | ||
mockFileExistsSyncFn.mockImplementationOnce(() => true); | ||
const sortedProject = { | ||
Project1: { | ||
root: 'project-one', | ||
}, | ||
Project2: { | ||
root: 'project-two', | ||
}, | ||
Project3: { | ||
root: 'project-three', | ||
}, | ||
}; | ||
|
||
// act | ||
const { | ||
json: { projects }, | ||
} = await verifyWorkspace(); | ||
const [project1, project2, project3] = Object.keys(projects); | ||
const [sorted1, sorted2, sorted3] = Object.keys(sortedProject); | ||
|
||
// assert | ||
expect(project1).toBe(sorted1); // should be 'Project1' | ||
expect(project2).toBe(sorted2); // should be 'Project2' | ||
expect(project3).toBe(sorted3); // should be 'Project3' | ||
}); | ||
}); | ||
}); |