|
| 1 | +import test from 'ava'; |
| 2 | +import onChange from '../source/index.js'; |
| 3 | + |
| 4 | +test('ArrayBuffer should not be proxied and byteLength should work', t => { |
| 5 | + const buffer = new ArrayBuffer(16); |
| 6 | + const object = {buffer}; |
| 7 | + |
| 8 | + let callCount = 0; |
| 9 | + const proxy = onChange(object, () => { |
| 10 | + callCount++; |
| 11 | + }); |
| 12 | + |
| 13 | + t.is(proxy.buffer.byteLength, 16); |
| 14 | + t.is(callCount, 0); |
| 15 | +}); |
| 16 | + |
| 17 | +test('SharedArrayBuffer should not be proxied and byteLength should work', t => { |
| 18 | + const buffer = new SharedArrayBuffer(24); |
| 19 | + const object = {buffer}; |
| 20 | + |
| 21 | + let callCount = 0; |
| 22 | + const proxy = onChange(object, () => { |
| 23 | + callCount++; |
| 24 | + }); |
| 25 | + |
| 26 | + t.is(proxy.buffer.byteLength, 24); |
| 27 | + t.is(callCount, 0); |
| 28 | +}); |
| 29 | + |
| 30 | +test('ArrayBuffer in nested object should work', t => { |
| 31 | + const buffer = new ArrayBuffer(32); |
| 32 | + const object = { |
| 33 | + nested: { |
| 34 | + data: { |
| 35 | + buffer, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }; |
| 39 | + |
| 40 | + let callCount = 0; |
| 41 | + const proxy = onChange(object, () => { |
| 42 | + callCount++; |
| 43 | + }); |
| 44 | + |
| 45 | + t.is(proxy.nested.data.buffer.byteLength, 32); |
| 46 | + t.is(callCount, 0); |
| 47 | +}); |
| 48 | + |
| 49 | +test('ArrayBuffer should be returned as-is, not proxied', t => { |
| 50 | + const buffer = new ArrayBuffer(8); |
| 51 | + const object = {buffer}; |
| 52 | + |
| 53 | + const proxy = onChange(object, () => {}); |
| 54 | + |
| 55 | + t.is(proxy.buffer, buffer); |
| 56 | +}); |
| 57 | + |
| 58 | +test('Replacing ArrayBuffer should trigger onChange', t => { |
| 59 | + const buffer1 = new ArrayBuffer(8); |
| 60 | + const buffer2 = new ArrayBuffer(16); |
| 61 | + const object = {buffer: buffer1}; |
| 62 | + |
| 63 | + let callCount = 0; |
| 64 | + const proxy = onChange(object, () => { |
| 65 | + callCount++; |
| 66 | + }); |
| 67 | + |
| 68 | + proxy.buffer = buffer2; |
| 69 | + t.is(callCount, 1); |
| 70 | + t.is(proxy.buffer.byteLength, 16); |
| 71 | +}); |
| 72 | + |
| 73 | +test('TypedArray views should still be proxied', t => { |
| 74 | + const uint8 = new Uint8Array([1, 2, 3]); |
| 75 | + const object = {view: uint8}; |
| 76 | + |
| 77 | + let callCount = 0; |
| 78 | + const proxy = onChange(object, () => { |
| 79 | + callCount++; |
| 80 | + }); |
| 81 | + |
| 82 | + t.is(proxy.view.length, 3); |
| 83 | + t.is(proxy.view.byteLength, 3); |
| 84 | + |
| 85 | + proxy.view[0] = 99; |
| 86 | + t.is(callCount, 1); |
| 87 | +}); |
| 88 | + |
| 89 | +test('ArrayBuffer from TypedArray.buffer should work', t => { |
| 90 | + const uint8 = new Uint8Array(16); |
| 91 | + const object = {view: uint8}; |
| 92 | + |
| 93 | + const proxy = onChange(object, () => {}); |
| 94 | + |
| 95 | + t.is(proxy.view.buffer.byteLength, 16); |
| 96 | +}); |
0 commit comments