-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
675 additions
and
428 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import _gl from 'gl'; | ||
import { createMouseEvent, getCanvas, sleep } from '../utils'; | ||
import '../useSnapshotMatchers'; | ||
import { Canvas, Circle } from '../../packages/core/src'; | ||
|
||
describe('Events', () => { | ||
['pointerDown', 'pointerUp', 'pointerMove'].forEach((type) => { | ||
it(`should listen to ${type} correctly.`, async () => { | ||
const $canvas = getCanvas(200, 200); | ||
$canvas.getBoundingClientRect = () => ({ | ||
x: 0, | ||
y: 0, | ||
top: 0, | ||
left: 0, | ||
width: 200, | ||
height: 200, | ||
bottom: 200, | ||
right: 200, | ||
toJSON: () => {}, | ||
}); | ||
|
||
const canvas = await new Canvas({ | ||
canvas: $canvas, | ||
setCursor: () => {}, | ||
}).initialized; | ||
const circle = new Circle({ | ||
cx: 100, | ||
cy: 100, | ||
r: 50, | ||
fill: 'red', | ||
}); | ||
canvas.appendChild(circle); | ||
canvas.render(); | ||
|
||
circle.addEventListener(type.toLowerCase(), () => { | ||
circle.fill = 'green'; | ||
canvas.render(); | ||
}); | ||
|
||
canvas.pluginContext.hooks[type].call( | ||
createMouseEvent(type.toLowerCase(), { clientX: 100, clientY: 100 }), | ||
); | ||
|
||
await sleep(1000); | ||
|
||
const dir = `${__dirname}/snapshots`; | ||
|
||
expect($canvas.getContext('webgl1')).toMatchWebGLSnapshot( | ||
dir, | ||
`events-${type.toLowerCase()}`, | ||
); | ||
|
||
canvas.destroy(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import xmlserializer from 'xmlserializer'; | ||
import { format } from 'prettier'; | ||
|
||
export type ToMatchSVGSnapshotOptions = { | ||
fileFormat?: string; | ||
}; | ||
|
||
// @see https://jestjs.io/docs/26.x/expect#expectextendmatchers | ||
export function toMatchSVGSnapshot( | ||
dom: SVGElement | null, | ||
dir: string, | ||
name: string, | ||
options: ToMatchSVGSnapshotOptions = {}, | ||
): { message: () => string; pass: boolean } { | ||
const { fileFormat = 'svg' } = options; | ||
const namePath = path.join(dir, name); | ||
const actualPath = path.join(dir, `${name}-actual.${fileFormat}`); | ||
const expectedPath = path.join(dir, `${name}.${fileFormat}`); | ||
|
||
let actual: string; | ||
try { | ||
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true }); | ||
|
||
actual = dom | ||
? format(xmlserializer.serializeToString(dom), { | ||
parser: 'babel', | ||
}) | ||
: 'null'; | ||
|
||
// Remove ';' after format by babel. | ||
if (actual !== 'null') actual = actual.slice(0, -2); | ||
|
||
if (!fs.existsSync(expectedPath)) { | ||
if (process.env.CI === 'true') { | ||
throw new Error(`Please generate golden image for ${namePath}`); | ||
} | ||
console.warn(`! generate ${namePath}`); | ||
fs.writeFileSync(expectedPath, actual); | ||
return { | ||
message: () => `generate ${namePath}`, | ||
pass: true, | ||
}; | ||
} else { | ||
const expected = fs.readFileSync(expectedPath, { | ||
encoding: 'utf8', | ||
flag: 'r', | ||
}); | ||
if (actual === expected) { | ||
if (fs.existsSync(actualPath)) fs.unlinkSync(actualPath); | ||
return { | ||
message: () => `match ${namePath}`, | ||
pass: true, | ||
}; | ||
} | ||
|
||
// Perverse actual file. | ||
if (actual) fs.writeFileSync(actualPath, actual); | ||
return { | ||
message: () => `mismatch ${namePath}`, | ||
pass: false, | ||
}; | ||
} | ||
} catch (e) { | ||
return { | ||
message: () => `${e}`, | ||
pass: false, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
__tests__/unit/snapshots/circle-stroke-alignment-inner.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions
29
__tests__/unit/snapshots/circle-stroke-alignment-outer.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.