Skip to content

Commit

Permalink
testing options
Browse files Browse the repository at this point in the history
  • Loading branch information
DanRDT committed Nov 25, 2023
1 parent 3f54ea4 commit 50a3776
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 19 deletions.
10 changes: 5 additions & 5 deletions src/utils/inputValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function validSvgPath(svgPath: SVGPathElement): Boolean {
} else if (svgPath.tagName !== 'path') {
console.error(`${svgPath.outerHTML} is not a path`)
return false
} else if (svgPath.getTotalLength() <= 0) {
} else if (svgPath.getTotalLength() <= 0 || typeof svgPath.getTotalLength() !== 'number') {
console.error(`${svgPath.outerHTML} has no length`)
return false
}
Expand All @@ -18,9 +18,9 @@ export function validSvgPath(svgPath: SVGPathElement): Boolean {

/**
* @param options default options merged with user options
* @param userOptions used to display user option errors
* @param userEnteredOptions used to display user option errors
* @returns the number of errors found in the provided options */
export function validateOptions(options: Options, userOptions: OptionalOptions): number {
export function validateOptions(options: Options, userEnteredOptions: OptionalOptions): number {
let errors = 0
try {
// check keys
Expand Down Expand Up @@ -71,7 +71,7 @@ export function validateOptions(options: Options, userOptions: OptionalOptions):

// Check offset
if (typeof options.offset !== 'number' || Number.isNaN(options.offset)) {
console.error(`Invalid offset option. Must be a number. Is currently ~ ${options.offset}`)
console.error(`Invalid offset option. Must be a number. Is currently ~ ${typeof options.offset} ~ ${options.offset}`)
errors++
}

Expand All @@ -88,7 +88,7 @@ export function validateOptions(options: Options, userOptions: OptionalOptions):
}

if (errors > 0) {
console.error(`Found ${errors} errors in animation options ~ ${JSON.stringify(userOptions)}`)
console.error(`Found ${errors} errors in animation options ~ ${JSON.stringify(userEnteredOptions)}`)
}
} catch (error) {
console.error(`Error validating options ~ ${error}`)
Expand Down
73 changes: 59 additions & 14 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,66 @@
import { describe, expect, it } from 'vitest'
import { JSDOM } from 'jsdom'
import { validSvgPath } from '../src/utils/inputValidation'
import scrollSvg, { scrollSvgNullable } from '../src'

describe('Vitest Test', () => {
it('Should always pass', () => {
expect(1).toBe(1)
})
})
const DOM = new JSDOM(
`<!DOCTYPE html>
<html lang="en" width="1080" height="1920">
<head></head>
<body width="1080" height="1920">
<svg id="svg" width="1080" height="1920" viewBox="0 0 9 699" fill="none" xmlns="http://www.w3.org/2000/svg">
<path id="svg-path" d="M 4 4 V 694.5" stroke="black" stroke-width="4" />
</svg>
</body>
</html>`,
{ pretendToBeVisual: true }
)
const { document } = DOM.window

const dom = new JSDOM(`<!DOCTYPE html>`)
const document = dom.window.document
const sampleSvgPath = document.querySelector('#svg-path') as SVGPathElement
// JSDOM doesn't support getTotalLength yet
sampleSvgPath.getTotalLength = () => 690
console.log(document)

// Don't Use outside of testing
/** Only for testing */
export function getSampleSvgPathForTesting() {
const exampleSvgPath = document.createElementNS('http://www.w3.org/2000/svg', 'path')
exampleSvgPath.setAttribute('d', 'M10 80 C 40 10, 65 10, 95 80 S 150 150, 180 80')
exampleSvgPath.setAttribute('stroke', 'black')
exampleSvgPath.setAttribute('stroke-width', '2')
exampleSvgPath.setAttribute('fill', 'none')
return exampleSvgPath
return sampleSvgPath
}

describe('Setup Tests', () => {
it('checks vitest', () => expect(1).toBe(1))

// SVG Validation
const svgPath = getSampleSvgPathForTesting()
it('checks getSampleSvgPathForTesting', () => {
expect(svgPath.tagName).toBe('path')
})

it('validates setupSvgPath', () => {
expect(1).toBe(1)
})

it('validates svg path validation', () => {
expect(validSvgPath(null as unknown as SVGPathElement)).toBe(false)
expect(validSvgPath(undefined as unknown as SVGPathElement)).toBe(false)

svgPath.getTotalLength = () => undefined as unknown as number
expect(validSvgPath(svgPath)).toBe(false)
svgPath.getTotalLength = () => 690
})
it('validates svg path', () => {
expect(validSvgPath(svgPath)).toBe(true)
})

it('validates scrollSvg', () => {
expect(scrollSvgNullable(undefined as unknown as any)).toBe(null)
})

it('validates getDrawOrigin', () => {
expect(1).toBe(1)
})

it('validates percentToPixelOffset', () => {
expect(1).toBe(1)
})
})
44 changes: 44 additions & 0 deletions test/options.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { describe, expect, it } from 'vitest'
import { defaultOptions } from '../src'
import { validateOptions } from '../src/utils/inputValidation'

describe('Options Tests', () => {
it('checks default options', () => {
expect(defaultOptions).toStrictEqual({
invert: false,
draw_origin: 'center',
offset: 0,
speed: 1,
undraw: false,
})
})
it('validates default options', () => {
expect(validateOptions(defaultOptions, {})).toBe(0)
})

it('validates options checker', () => {
expect(validateOptions({ ...defaultOptions, option: 'true' } as unknown as any, {})).toBe(1)
// invert
expect(validateOptions({ ...defaultOptions, invert: 'true' } as unknown as any, {})).toBe(1)
expect(validateOptions({ ...defaultOptions, invert: true }, {})).toBe(0)
// draw origin
expect(validateOptions({ ...defaultOptions, draw_origin: 'middle' } as unknown as any, {})).toBe(1)
expect(validateOptions({ ...defaultOptions, draw_origin: NaN }, {})).toBe(1)
expect(validateOptions({ ...defaultOptions, draw_origin: -0.5 }, {})).toBe(1)
expect(validateOptions({ ...defaultOptions, draw_origin: 1.5 }, {})).toBe(1)
expect(validateOptions({ ...defaultOptions, draw_origin: 0.3 }, {})).toBe(0)
expect(validateOptions({ ...defaultOptions, draw_origin: 'top' }, {})).toBe(0)
// offset
expect(validateOptions({ ...defaultOptions, offset: '100' } as unknown as any, {})).toBe(1)
expect(validateOptions({ ...defaultOptions, offset: NaN }, {})).toBe(1)
expect(validateOptions({ ...defaultOptions, offset: 100 }, {})).toBe(0)
// speed
expect(validateOptions({ ...defaultOptions, speed: '100' } as unknown as any, {})).toBe(1)
expect(validateOptions({ ...defaultOptions, speed: NaN }, {})).toBe(1)
expect(validateOptions({ ...defaultOptions, speed: -1 }, {})).toBe(1)
expect(validateOptions({ ...defaultOptions, speed: 2 }, {})).toBe(0)
// undraw
expect(validateOptions({ ...defaultOptions, undraw: 'true' } as unknown as any, {})).toBe(1)
expect(validateOptions({ ...defaultOptions, undraw: true }, {})).toBe(0)
})
})

0 comments on commit 50a3776

Please sign in to comment.