|
| 1 | +import 'vscode-test'; |
| 2 | +import 'mocha'; |
| 3 | +import * as sinon from 'sinon'; |
| 4 | +import { expect } from 'chai'; |
| 5 | +import { ExtensionContext } from 'vscode'; |
| 6 | + |
| 7 | +import { DatabaseEventKind, DatabaseItem, DatabaseManager } from '../../databases'; |
| 8 | +import { QueryServerConfig } from '../../config'; |
| 9 | +import { Logger } from '../../logging'; |
| 10 | + |
| 11 | +describe('databases', () => { |
| 12 | + let databaseManager: DatabaseManager; |
| 13 | + let updateSpy: sinon.SinonSpy; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + updateSpy = sinon.spy(); |
| 17 | + databaseManager = new DatabaseManager( |
| 18 | + { |
| 19 | + workspaceState: { |
| 20 | + update: updateSpy |
| 21 | + } |
| 22 | + } as unknown as ExtensionContext, |
| 23 | + {} as QueryServerConfig, |
| 24 | + {} as Logger, |
| 25 | + ); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should fire events when adding and removing a db item', () => { |
| 29 | + const mockDbItem = { |
| 30 | + databaseUri: 'file:/abc', |
| 31 | + getPersistedState() { |
| 32 | + return this.databaseUri; |
| 33 | + } |
| 34 | + }; |
| 35 | + const spy = sinon.spy(); |
| 36 | + databaseManager.onDidChangeDatabaseItem(spy); |
| 37 | + (databaseManager as any).addDatabaseItem(mockDbItem); |
| 38 | + |
| 39 | + expect((databaseManager as any)._databaseItems).to.deep.eq([mockDbItem]); |
| 40 | + expect(updateSpy).to.have.been.calledWith('databaseList', ['file:/abc']); |
| 41 | + expect(spy).to.have.been.calledWith({ |
| 42 | + item: undefined, |
| 43 | + kind: DatabaseEventKind.Add |
| 44 | + }); |
| 45 | + |
| 46 | + sinon.reset(); |
| 47 | + |
| 48 | + // now remove the item |
| 49 | + databaseManager.removeDatabaseItem(mockDbItem as unknown as DatabaseItem); |
| 50 | + expect((databaseManager as any)._databaseItems).to.deep.eq([]); |
| 51 | + expect(updateSpy).to.have.been.calledWith('databaseList', []); |
| 52 | + expect(spy).to.have.been.calledWith({ |
| 53 | + item: undefined, |
| 54 | + kind: DatabaseEventKind.Remove |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should rename a db item and emit an event', () => { |
| 59 | + const mockDbItem = { |
| 60 | + databaseUri: 'file:/abc', |
| 61 | + name: 'abc', |
| 62 | + getPersistedState() { |
| 63 | + return this.name; |
| 64 | + } |
| 65 | + }; |
| 66 | + const spy = sinon.spy(); |
| 67 | + databaseManager.onDidChangeDatabaseItem(spy); |
| 68 | + (databaseManager as any).addDatabaseItem(mockDbItem); |
| 69 | + |
| 70 | + databaseManager.renameDatabaseItem(mockDbItem as unknown as DatabaseItem, 'new name'); |
| 71 | + |
| 72 | + expect(mockDbItem.name).to.eq('new name'); |
| 73 | + expect(updateSpy).to.have.been.calledWith('databaseList', ['new name']); |
| 74 | + expect(spy).to.have.been.calledWith({ |
| 75 | + item: undefined, |
| 76 | + kind: DatabaseEventKind.Rename |
| 77 | + }); |
| 78 | + }); |
| 79 | +}); |
0 commit comments