diff --git a/extension/src/test/suite/connect/index.test.ts b/extension/src/test/suite/connect/index.test.ts index a0d8523a07..4abe912257 100644 --- a/extension/src/test/suite/connect/index.test.ts +++ b/extension/src/test/suite/connect/index.test.ts @@ -6,9 +6,11 @@ import { ExtensionContext, SecretStorage, Uri, + WorkspaceConfiguration, commands, env, - window + window, + workspace } from 'vscode' import { Disposable } from '../../../extension' import { @@ -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() @@ -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( diff --git a/webview/src/connect/components/App.test.tsx b/webview/src/connect/components/App.test.tsx index bebb930f17..d4bdf8039f 100644 --- a/webview/src/connect/components/App.test.tsx +++ b/webview/src/connect/components/App.test.tsx @@ -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' @@ -18,47 +22,79 @@ afterEach(() => { cleanup() }) -const renderApp = () => { - return render() +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() + 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 + }) }) }) })