Skip to content

Commit

Permalink
add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mattseddon committed Jul 14, 2023
1 parent 600b692 commit 82667e6
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 16 deletions.
20 changes: 5 additions & 15 deletions extension/src/test/suite/experiments/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
configurationChangeEvent,
experimentsUpdatedEvent,
extensionUri,
getActiveEditorUpdatedEvent,
getInputBoxEvent,
getMessageReceivedEmitter
} from '../util'
Expand Down Expand Up @@ -1893,17 +1894,6 @@ suite('Experiments Test Suite', () => {
})

describe('editor/title icons', () => {
const getActiveEditorUpdatedEvent = () =>
new Promise(resolve => {
const listener = disposable.track(
window.onDidChangeActiveTextEditor(() => {
resolve(undefined)
disposable.untrack(listener)
listener.dispose()
})
)
})

it('should set the appropriate context value when a params file is open in the active editor/closed', async () => {
const paramsFile = Uri.file(join(dvcDemoPath, 'params.yaml'))
await window.showTextDocument(paramsFile)
Expand All @@ -1928,7 +1918,7 @@ suite('Experiments Test Suite', () => {

mockSetContextValue.resetHistory()

const startupEditorClosed = getActiveEditorUpdatedEvent()
const startupEditorClosed = getActiveEditorUpdatedEvent(disposable)

await closeAllEditors()
await startupEditorClosed
Expand All @@ -1940,12 +1930,12 @@ suite('Experiments Test Suite', () => {

mockSetContextValue.resetHistory()

const activeEditorUpdated = getActiveEditorUpdatedEvent()
const activeEditorUpdated = getActiveEditorUpdatedEvent(disposable)

await window.showTextDocument(paramsFile)
await activeEditorUpdated

const activeEditorClosed = getActiveEditorUpdatedEvent()
const activeEditorClosed = getActiveEditorUpdatedEvent(disposable)

expect(
mockContext['dvc.experiments.file.active'],
Expand All @@ -1971,7 +1961,7 @@ suite('Experiments Test Suite', () => {
await experiments.isReady()

expect(setContextValueSpy).not.to.be.calledWith(
VscodeContext.ContextKey.EXPERIMENTS_FILE_ACTIVE
'dvc.experiments.file.active'
)
})
})
Expand Down
76 changes: 75 additions & 1 deletion extension/src/test/suite/pipeline/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { join } from 'path'
import { afterEach, beforeEach, describe, it, suite } from 'mocha'
import { SinonStub, restore, stub } from 'sinon'
import { SinonStub, restore, spy, stub } from 'sinon'
import { expect } from 'chai'
import { QuickPickItem, Uri, window } from 'vscode'
import { buildPipeline } from './util'
import {
bypassProcessManagerDebounce,
closeAllEditors,
getActiveEditorUpdatedEvent,
getMockNow
} from '../util'
import { Disposable } from '../../../extension'
Expand All @@ -15,6 +16,7 @@ import * as QuickPick from '../../../vscode/quickPick'
import { QuickPickOptionsWithTitle } from '../../../vscode/quickPick'
import * as FileSystem from '../../../fileSystem'
import { ScriptCommand } from '../../../pipeline'
import * as VscodeContext from '../../../vscode/context'

suite('Pipeline Test Suite', () => {
const disposable = Disposable.fn()
Expand Down Expand Up @@ -285,4 +287,76 @@ suite('Pipeline Test Suite', () => {
expect(mockFindOrCreateDvcYamlFile).not.to.be.called
})
})

it('should set the appropriate context value when a dvc.yaml is open in the active editor', async () => {
const dvcYaml = Uri.file(join(dvcDemoPath, 'dvc.yaml'))
await window.showTextDocument(dvcYaml)

const mockContext: { [key: string]: unknown } = {
'dvc.pipeline.file.active': false
}

const mockSetContextValue = stub(VscodeContext, 'setContextValue')
mockSetContextValue.callsFake((key: string, value: unknown) => {
mockContext[key] = value
return Promise.resolve(undefined)
})

const { pipeline } = buildPipeline({ disposer: disposable })
await pipeline.isReady()

expect(
mockContext['dvc.pipeline.file.active'],
'should set dvc.pipeline.file.active to true when a dvc.yaml is open and the extension starts'
).to.be.true

mockSetContextValue.resetHistory()

const startupEditorClosed = getActiveEditorUpdatedEvent(disposable)

await closeAllEditors()
await startupEditorClosed

expect(
mockContext['dvc.pipeline.file.active'],
'should set dvc.pipeline.file.active to false when the dvc.yaml in the active editor is closed'
).to.be.false

mockSetContextValue.resetHistory()

const activeEditorUpdated = getActiveEditorUpdatedEvent(disposable)

await window.showTextDocument(dvcYaml)
await activeEditorUpdated

const activeEditorClosed = getActiveEditorUpdatedEvent(disposable)

expect(
mockContext['dvc.pipeline.file.active'],
'should set dvc.pipeline.file.active to true when a dvc.yaml file is in the active editor'
).to.be.true

await closeAllEditors()
await activeEditorClosed

expect(
mockContext['dvc.pipeline.file.active'],
'should set dvc.pipeline.file.active to false when the dvc.yaml in the active editor is closed again'
).to.be.false
})

it('should set dvc.pipeline.file.active to false when a dvc.yaml is not open and the extension starts', async () => {
const nonDvcYaml = Uri.file(join(dvcDemoPath, '.gitignore'))
await window.showTextDocument(nonDvcYaml)

const setContextValueSpy = spy(VscodeContext, 'setContextValue')

const { pipeline } = buildPipeline({ disposer: disposable })
await pipeline.isReady()

expect(setContextValueSpy).to.be.calledWith(
'dvc.pipeline.file.active',
false
)
})
})
11 changes: 11 additions & 0 deletions extension/src/test/suite/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,14 @@ export const waitForEditorText = async (): Promise<unknown> => {
}
return waitForEditorText()
}

export const getActiveEditorUpdatedEvent = (disposer: Disposer) =>
new Promise(resolve => {
const listener = disposer.track(
window.onDidChangeActiveTextEditor(() => {
resolve(undefined)
disposer.untrack(listener)
listener.dispose()
})
)
})

0 comments on commit 82667e6

Please sign in to comment.