|
| 1 | +import type { MatcherContext } from 'expect' |
| 2 | +import type { TreeItem } from 'wdio-vscode-service' |
| 3 | +import type { STATUS } from '../helpers.js' |
| 4 | + |
| 5 | +export interface ExpectedTreeItem { |
| 6 | + label: string |
| 7 | + status: StatusStrings |
| 8 | + children?: ExpectedTreeItem[] |
| 9 | +} |
| 10 | + |
| 11 | +export type StatusStrings = (typeof STATUS)[keyof typeof STATUS] |
| 12 | + |
| 13 | +class MismatchTreeStructureError extends Error { |
| 14 | + constructor(public assertionMessage: () => string) { |
| 15 | + super('Mismatch') |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +async function expectTreeToMatchStructure( |
| 20 | + this: MatcherContext, |
| 21 | + receivedTree: TreeItem[], |
| 22 | + expectedTree: ExpectedTreeItem[], |
| 23 | + level = 0, |
| 24 | + index = 0 |
| 25 | +) { |
| 26 | + try { |
| 27 | + if (expectedTree.length !== receivedTree.length) { |
| 28 | + throw new MismatchTreeStructureError( |
| 29 | + () => |
| 30 | + `Mismatch the number of items at (${index} : ${level})` + |
| 31 | + ` Expected: ${this.utils.printExpected(expectedTree.length)}` + |
| 32 | + ` Received: ${this.utils.printReceived(receivedTree.length)}` |
| 33 | + ) |
| 34 | + } |
| 35 | + |
| 36 | + for (const [index, item] of Object.entries(receivedTree)) { |
| 37 | + const rootLabel = await item.getLabel() |
| 38 | + |
| 39 | + const expectItem = expectedTree[Number(index)] |
| 40 | + const labelRegex = new RegExp(expectItem.label) |
| 41 | + if (!labelRegex.test(rootLabel)) { |
| 42 | + throw new MismatchTreeStructureError( |
| 43 | + () => |
| 44 | + `Mismatch the label of items at (${index} : ${level})` + |
| 45 | + ` Expected: ${this.utils.printExpected(expectItem.label)}` + |
| 46 | + ` Received: ${this.utils.printReceived(rootLabel)}` |
| 47 | + ) |
| 48 | + } |
| 49 | + const receivedStatus = rootLabel.match(/\(([^)]+)\)/) |
| 50 | + if (!receivedStatus || receivedStatus[1] !== expectItem.status) { |
| 51 | + throw new MismatchTreeStructureError( |
| 52 | + () => |
| 53 | + `Mismatch the status of items at (${index} : ${level})` + |
| 54 | + ` Expected: ${this.utils.printExpected(expectItem.status)}` + |
| 55 | + ` Received: ${this.utils.printReceived(receivedStatus![1])}` + |
| 56 | + ` (${rootLabel})` |
| 57 | + ) |
| 58 | + } |
| 59 | + |
| 60 | + if (!(await item.isExpanded())) { |
| 61 | + await item.expand() |
| 62 | + } |
| 63 | + |
| 64 | + const children = await item.getChildren() |
| 65 | + const expectedChildren = expectItem.children |
| 66 | + if (children && expectedChildren) { |
| 67 | + await expectTreeToMatchStructure.call(this, children, expectedChildren, level + 1, Number(index)) |
| 68 | + } |
| 69 | + } |
| 70 | + return { |
| 71 | + pass: true, |
| 72 | + message: () => 'All ok.', |
| 73 | + } |
| 74 | + } catch (error) { |
| 75 | + if (level > 0) { |
| 76 | + throw error |
| 77 | + } |
| 78 | + const message = error instanceof MismatchTreeStructureError ? error.assertionMessage : () => String(error) |
| 79 | + return { |
| 80 | + pass: false, |
| 81 | + message, |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +try { |
| 87 | + if (typeof expect !== 'undefined' && expect.extend) { |
| 88 | + expect.extend({ |
| 89 | + async toMatchTreeStructure(tree: TreeItem[], expectedStructure: ExpectedTreeItem[]) { |
| 90 | + return await expectTreeToMatchStructure.call(this as unknown as MatcherContext, tree, expectedStructure) |
| 91 | + }, |
| 92 | + }) |
| 93 | + } |
| 94 | +} catch (error) { |
| 95 | + console.warn('Failed to extend expect:', error) |
| 96 | +} |
0 commit comments