Skip to content

Commit

Permalink
WIP: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sidnioulz committed Jan 29, 2024
1 parent c7c3639 commit 9bef4ac
Showing 1 changed file with 75 additions and 4 deletions.
79 changes: 75 additions & 4 deletions src/template/__tests__/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -463,7 +463,6 @@ describe('template', () => {
expect(api.compareAttributeValues(null, api.createText({ content: 'unrelated' }))).toBe(
false,
)
// TODO
})

it('correctly discriminates unrelated TextNodes', () => {
Expand Down Expand Up @@ -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', () => {

Check failure on line 540 in src/template/__tests__/api.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected focused test
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
})

Expand Down

0 comments on commit 9bef4ac

Please sign in to comment.