MCPcopy
hub / github.com/fabricjs/fabric.js / assertCanvasDisposing

Function assertCanvasDisposing

test/unit/canvas_dispose.js:7–203  ·  view source on GitHub ↗
(klass)

Source from the content-addressed store, hash-verified

5}
6
7function assertCanvasDisposing(klass) {
8
9 QUnit.test('dispose', async function (assert) {
10 const done = assert.async();
11 const canvas = new klass(null, { renderOnAddRemove: false });
12 assert.notOk(canvas.destroyed, 'should not have been destroyed yet');
13 await canvas.dispose();
14 assert.ok(canvas.destroyed, 'should have flagged `destroyed`');
15 done();
16 });
17
18 QUnit.test('dispose: clear references sync', function (assert) {
19 var el = fabric.getFabricDocument().createElement('canvas'),
20 parentEl = fabric.getFabricDocument().createElement('div');
21
22 el.width = 200; el.height = 200;
23 parentEl.className = 'rootNode';
24 parentEl.appendChild(el);
25
26 fabric.config.configure({ devicePixelRatio: 1.25 });
27
28 el.style.position = 'relative';
29 var elStyle = el.style.cssText;
30 assert.equal(elStyle, 'position: relative;', 'el style should not be empty');
31
32 var canvas = new fabric.Canvas(el, { enableRetinaScaling: true, renderOnAddRemove: false });
33 assert.equal(canvas.elements._originalCanvasStyle, elStyle, 'saved original canvas style for disposal');
34 assert.notEqual(el.style.cssText, canvas.elements._originalCanvasStyle, 'canvas el style has been changed');
35 assert.equal(el.getAttribute('data-fabric'), 'main', 'lowerCanvasEl should be marked by fabric');
36 assert.ok(typeof canvas.dispose === 'function');
37 assert.ok(typeof canvas.destroy === 'function');
38 canvas.add(makeRect(), makeRect(), makeRect());
39 canvas.item(0).animate({ scaleX: 10 });
40 assert.equal(fabric.runningAnimations.length, 1, 'should have a running animation');
41 canvas.dispose();
42 assert.equal(canvas.disposed, true, 'dispose should flag disposed');
43 assert.equal(el.hasAttribute('data-fabric'), false, 'dispose should clear lowerCanvasEl data-fabric attr');
44 assert.equal(canvas.elements._originalCanvasStyle, undefined, 'removed original canvas style');
45 assert.equal(el.style.cssText, elStyle, 'restored original canvas style');
46 assert.equal(el.width, 200, 'restored width');
47 assert.equal(el.height, 200, 'restored height');
48 });
49
50
51 QUnit.test('dispose: clear references async', async function (assert) {
52 const canvas = new klass(null, { renderOnAddRemove: false });
53 assert.ok(typeof canvas.dispose === 'function');
54 assert.ok(typeof canvas.destroy === 'function');
55 canvas.add(makeRect(), makeRect(), makeRect());
56 const lowerCanvas = canvas.lowerCanvasEl;
57 assert.equal(lowerCanvas.getAttribute('data-fabric'), 'main', 'lowerCanvasEl should be marked by fabric');
58 await canvas.dispose();
59 assert.equal(canvas.destroyed, true, 'dispose should flag destroyed');
60 assert.equal(canvas.getObjects().length, 0, 'dispose should clear canvas');
61 assert.equal(canvas.lowerCanvasEl, null, 'dispose should clear lowerCanvasEl');
62 assert.equal(lowerCanvas.hasAttribute('data-fabric'), false, 'dispose should clear lowerCanvasEl data-fabric attr');
63 assert.equal(canvas.contextContainer, null, 'dispose should clear contextContainer');
64 });

Callers 2

testCanvasDisposingFunction · 0.85

Calls 14

doneFunction · 0.85
testImageDataFunction · 0.85
configureMethod · 0.80
itemMethod · 0.80
getObjectsMethod · 0.80
onMethod · 0.80
fireMethod · 0.80
makeRectFunction · 0.70
animateFunction · 0.70
disposeMethod · 0.45
addMethod · 0.45
requestRenderAllMethod · 0.45

Tested by

no test coverage detected