Skip to content

Commit

Permalink
Update test for toc updates
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerZeroMaster committed Aug 8, 2024
1 parent 10ec38a commit 6420033
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
14 changes: 6 additions & 8 deletions client/specs/extension.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import { join } from 'path'
import { expect } from '@jest/globals'
import mockfs from 'mock-fs'

import { activate, deactivate, forwardOnDidChangeWorkspaceFolders, setLanguageServerLauncher, setResourceRootDir } from '../src/extension'
import { activate, deactivate, forwardOnDidChangeWorkspaceFolders, getTocTree, setLanguageServerLauncher, setResourceRootDir } from '../src/extension'
import { type Extension, type ExtensionContext, type WebviewPanel } from 'vscode'
import * as vscode from 'vscode'
import * as utils from '../src/utils' // Used for dependency mocking in tests
import Sinon from 'sinon'
import { type LanguageClient } from 'vscode-languageclient/node'
import { OpenstaxCommand } from '../src/extension-types'
import { type TocEditorPanel } from '../src/panel-toc-editor'
import { type BooksAndOrphans, ExtensionServerNotification } from '../../common/src/requests'
import { type PanelManager } from '../src/panel'
import { type CnxmlPreviewPanel } from '../src/panel-cnxml-preview'
Expand Down Expand Up @@ -53,20 +52,19 @@ describe('Extension', () => {
it('Starts up', async function () {
await expect(activate(extensionContext)).resolves.toBeTruthy()
})
it('updates the TocPanel when the language server sends a BookTocs Notification', async () => {
it('updates the TreeView when the language server sends a BookTocs Notification', async () => {
setResourceRootDir(join(__dirname, '..', 'static'))

const extensions = await activate(extensionContext)
const pm = extensions[OpenstaxCommand.SHOW_TOC_EDITOR]
expect(pm.panel()).toBeNull()
await activate(extensionContext)

expect(onNotificationStub.firstCall.args[0]).toBe(ExtensionServerNotification.BookTocs)
const cb = onNotificationStub.firstCall.args[1]
const params: BooksAndOrphans = { books: [], orphans: [] }
cb(params)

const panel = pm.newPanel() as TocEditorPanel
const updateStub = sinon.stub(panel, 'update')
const { tocTreesProvider } = getTocTree()
if (tocTreesProvider == null) throw new Error('tocTreesProvider was not set yet')
const updateStub = sinon.stub(tocTreesProvider, 'update')
cb(params)

expect(updateStub.callCount).toBe(1)
Expand Down
5 changes: 5 additions & 0 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export function setLanguageServerLauncher(l: typeof languageServerLauncher) {
export const forwardOnDidChangeWorkspaceFolders = (clientInner: LanguageClient) => async (event: vscode.WorkspaceFoldersChangeEvent) => {
await clientInner.sendRequest('onDidChangeWorkspaceFolders', event)
}
export const getTocTree = (): {
tocTreesView: vscode.TreeView<BookOrTocNode> | undefined | null
tocTreesProvider: TocsTreeProvider | undefined | null
tocEventHandler: TocsEventHandler | undefined | null
} => ({ tocTreesView, tocTreesProvider, tocEventHandler })

type ExtensionExports = { [key in OpenstaxCommand]: PanelManager<Panel<unknown, unknown, unknown>> }
export async function activate(context: vscode.ExtensionContext): Promise<ExtensionExports> {
Expand Down
2 changes: 1 addition & 1 deletion client/src/panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export abstract class Panel<InMessage, OutMessage, State> implements DisposableS
this.panel.onDidDispose(() => { this.dispose() })

this.registerDisposable(this.panel.webview.onDidReceiveMessage((message) => {
/* istanbul ignore if */
/* istanbul ignore next */
if (message.type === PanelStateMessageType.Request) {
void ensureCatchPromise(this.sendState())
} else {
Expand Down

0 comments on commit 6420033

Please sign in to comment.