From 9bef4ace51e6dbb1450ee5d61a430ea249dca4f3 Mon Sep 17 00:00:00 2001 From: Steve Dodier-Lazaro Date: Mon, 29 Jan 2024 12:11:10 +0100 Subject: [PATCH] WIP: tests --- src/template/__tests__/api.spec.ts | 79 ++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 4 deletions(-) diff --git a/src/template/__tests__/api.spec.ts b/src/template/__tests__/api.spec.ts index cbec155..08d9094 100644 --- a/src/template/__tests__/api.spec.ts +++ b/src/template/__tests__/api.spec.ts @@ -5,7 +5,7 @@ import { compileTemplate } from '@vue/compiler-sfc' import SampleOne from '../../__fixtures__/One' import * as api from '../api' import { stringifyNode, type StringifiableNode } from '../stringify' -import { genFakeLoc } from '../utils' +import { genFakeLoc, isAttribute } from '../utils' function prepare(source: string) { return compileTemplate({ @@ -463,7 +463,6 @@ describe('template', () => { expect(api.compareAttributeValues(null, api.createText({ content: 'unrelated' }))).toBe( false, ) - // TODO }) it('correctly discriminates unrelated TextNodes', () => { @@ -494,10 +493,82 @@ describe('template', () => { }) describe('exploreAst', () => { - // TODO + const ast = prepare(SampleOne) + + it.each([ + ['when matching nothing', () => false], + ['when matching some nodes', (node) => node.type % 2], + ['when matching everything', () => true], + ])('calls the matcher function for every node %s', (description, matcherImplem) => { + const matcher = jest.fn().mockImplementation(matcherImplem) + + api.exploreAst(ast, matcher) + + expect(matcher).toHaveBeenCalledTimes(181) + }) + + it('provides nodes to the matcher function', () => { + const matcher = jest.fn() + + api.exploreAst(ast, matcher) + + expect(matcher).toHaveBeenCalledWith(ast.children[0]) + }) + + it('returns only the nodes for which the matcher returned true', () => { + const matcher = jest.fn().mockImplementation((node) => node === ast.children[0]) + + const outcome = api.exploreAst(ast, matcher) + + expect(outcome).toHaveLength(1) + expect(outcome[0]).toBe(ast.children[0]) + }) + + it('doesn\'t return the same node twice', () => { + const matcher = jest.fn().mockReturnValue(true) + + const neverSameNodeCache = new Set() + const output = api.exploreAst(ast, matcher) + + for (const found of output) { + expect(neverSameNodeCache.has(found)).toBe(false) + neverSameNodeCache.add(found) + } + }) }) - describe('findAstAttributes', () => { + describe.only('findAstAttributes', () => { + const ast = prepare(SampleOne) + + it('calls exploreAst and findAttributes', () => { + const exploreSpy = jest.spyOn(api, 'exploreAst') + + api.findAstAttributes(ast, () => false) + + expect(exploreSpy).toHaveBeenCalled() + }) + + it('only calls the matcher on attributes', () => { + api.findAstAttributes(ast, (node) => { + expect(isAttribute(node)).toBeTruthy() + + return true + }) + + expect.assertions(15) + }) + + it('calls the matcher on all attributes', () => { + api.findAstAttributes(ast, ((node) => { + expect(isAttribute(node)).toBeTruthy() + + return true + })) + + + + expect.assertions(15) + }) // TODO })