Skip to content

Commit

Permalink
add extra tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mattseddon committed Mar 3, 2023
1 parent 188246e commit 62c67de
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 34 deletions.
26 changes: 25 additions & 1 deletion extension/src/test/suite/connect/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import {
ExtensionContext,
SecretStorage,
Uri,
WorkspaceConfiguration,
commands,
env,
window
window,
workspace
} from 'vscode'
import { Disposable } from '../../../extension'
import {
Expand All @@ -22,6 +24,7 @@ import { WEBVIEW_TEST_TIMEOUT } from '../timeouts'
import { ContextKey } from '../../../vscode/context'
import { STUDIO_ACCESS_TOKEN_KEY } from '../../../connect/token'
import { RegisteredCommands } from '../../../commands/external'
import { ConfigKey } from '../../../vscode/config'

suite('Connect Test Suite', () => {
const disposable = Disposable.fn()
Expand Down Expand Up @@ -154,6 +157,27 @@ suite('Connect Test Suite', () => {
)
}).timeout(WEBVIEW_TEST_TIMEOUT)

it('should handle a message to set dvc.studio.shareExperimentsLive', async () => {
const { connect, mockMessageReceived } = await buildConnect()
await connect.isReady()

const mockUpdate = stub()

stub(workspace, 'getConfiguration').returns({
update: mockUpdate
} as unknown as WorkspaceConfiguration)

mockMessageReceived.fire({
payload: false,
type: MessageFromWebviewType.SET_STUDIO_SHARE_EXPERIMENTS_LIVE
})

expect(mockUpdate).to.be.calledWithExactly(
ConfigKey.STUDIO_SHARE_EXPERIMENTS_LIVE,
false
)
})

it('should be able to delete the Studio access token from secrets storage', async () => {
const mockDelete = stub()
stub(
Expand Down
102 changes: 69 additions & 33 deletions webview/src/connect/components/App.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import React from 'react'
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
import { MessageFromWebviewType } from 'dvc/src/webview/contract'
import {
MessageFromWebviewType,
MessageToWebviewType
} from 'dvc/src/webview/contract'
import { ConnectData } from 'dvc/src/connect/webview/contract'
import { App } from './App'
import { vsCodeApi } from '../../shared/api'

Expand All @@ -18,47 +22,79 @@ afterEach(() => {
cleanup()
})

const renderApp = () => {
return render(<App />)
const setData = ({ isStudioConnected, shareLiveToStudio }: ConnectData) => {
fireEvent(
window,
new MessageEvent('message', {
data: {
data: {
isStudioConnected,
shareLiveToStudio
},
type: MessageToWebviewType.SET_DATA
}
})
)
}

const renderApp = (isStudioConnected = false, shareLiveToStudio = false) => {
render(<App />)
setData({ isStudioConnected, shareLiveToStudio })
}

describe('App', () => {
it('should show three buttons which walk the user through saving a token', async () => {
renderApp()
const buttons = await screen.findAllByRole('button')
expect(buttons).toHaveLength(3)
})
describe('Studio not connected', () => {
it('should show three buttons which walk the user through saving a token', async () => {
renderApp()
const buttons = await screen.findAllByRole('button')
expect(buttons).toHaveLength(3)
})

it('should show a button which opens Studio', () => {
renderApp()
mockPostMessage.mockClear()
const button = screen.getByText('Sign In')
fireEvent.click(button)
expect(mockPostMessage).toHaveBeenCalledTimes(1)
expect(mockPostMessage).toHaveBeenCalledWith({
type: MessageFromWebviewType.OPEN_STUDIO
it('should show a button which opens Studio', () => {
renderApp()
mockPostMessage.mockClear()
const button = screen.getByText('Sign In')
fireEvent.click(button)
expect(mockPostMessage).toHaveBeenCalledTimes(1)
expect(mockPostMessage).toHaveBeenCalledWith({
type: MessageFromWebviewType.OPEN_STUDIO
})
})

it("should show a button which opens the user's Studio profile", () => {
renderApp()
mockPostMessage.mockClear()
const button = screen.getByText('Get Token')
fireEvent.click(button)
expect(mockPostMessage).toHaveBeenCalledTimes(1)
expect(mockPostMessage).toHaveBeenCalledWith({
type: MessageFromWebviewType.OPEN_STUDIO_PROFILE
})
})
})

it("should show a button which opens the user's Studio profile", () => {
renderApp()
mockPostMessage.mockClear()
const button = screen.getByText('Get Token')
fireEvent.click(button)
expect(mockPostMessage).toHaveBeenCalledTimes(1)
expect(mockPostMessage).toHaveBeenCalledWith({
type: MessageFromWebviewType.OPEN_STUDIO_PROFILE
it("should show a button which allows the user's to save their Studio access token", () => {
renderApp()
mockPostMessage.mockClear()
const button = screen.getByText('Save')
fireEvent.click(button)
expect(mockPostMessage).toHaveBeenCalledTimes(1)
expect(mockPostMessage).toHaveBeenCalledWith({
type: MessageFromWebviewType.SAVE_STUDIO_TOKEN
})
})
})

it("should show a button which allows the user's to save their Studio access token", () => {
renderApp()
mockPostMessage.mockClear()
const button = screen.getByText('Save')
fireEvent.click(button)
expect(mockPostMessage).toHaveBeenCalledTimes(1)
expect(mockPostMessage).toHaveBeenCalledWith({
type: MessageFromWebviewType.SAVE_STUDIO_TOKEN
describe('Studio connected', () => {
it('should render a checkbox which can be used to update dvc.studio.shareExperimentsLive', () => {
const shareExperimentsLive = false
renderApp(true, shareExperimentsLive)
mockPostMessage.mockClear()
const checkbox = screen.getByRole('checkbox')
fireEvent.click(checkbox)
expect(mockPostMessage).toHaveBeenCalledWith({
payload: !shareExperimentsLive,
type: MessageFromWebviewType.SET_STUDIO_SHARE_EXPERIMENTS_LIVE
})
})
})
})

0 comments on commit 62c67de

Please sign in to comment.