Skip to content

Commit

Permalink
Add regression tests for jsifying props containing frozen objects
Browse files Browse the repository at this point in the history
  • Loading branch information
greglittlefield-wf committed Aug 10, 2022
1 parent 020a1a7 commit d609e0c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
80 changes: 80 additions & 0 deletions test/react_client/js_interop_helpers_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class Foo {
external num bar();
}

@JS()
external dynamic createArray();

main() {
group('jsifyAndAllowInterop', () {
test('converts a List', () {
Expand Down Expand Up @@ -131,5 +134,82 @@ main() {
expect(objectKeys(convertedNestedJsObject), expectedProperties,
reason: 'JS object should not have any additional properties');
});

group('works as expected when the map contains frozen objects:', () {
test('anonymous object', () {
final frozenAnonymousObject = jsify({'foo': 'bar'});
expect(_getPrototypeOf(frozenAnonymousObject), _objectPrototype,
reason: 'test setup check; should not extend from another JS type');

_freeze(frozenAnonymousObject);
expect(_isFrozen(frozenAnonymousObject), isTrue, reason: 'test setup check; should have frozen');

dynamic jsObject;
expect(() {
jsObject = jsifyAndAllowInterop({
'frozenObject': frozenAnonymousObject,
});
}, returnsNormally, reason: 'should not throw when it encounters a frozen object');
final convertedNestedFrozenObject = getProperty(jsObject, 'frozenObject');
expect(convertedNestedFrozenObject, same(frozenAnonymousObject),
reason: 'JS object should have just gotten passed through');
});

test('non-anonymous object', () {
final frozenNonAnynymousObject = Foo(21);
expect(_getPrototypeOf(frozenNonAnynymousObject), isNot(_objectPrototype),
reason: 'test setup check; should extend from another JS type');

_freeze(frozenNonAnynymousObject);
expect(_isFrozen(frozenNonAnynymousObject), isTrue, reason: 'test setup check; should have frozen');

dynamic jsObject;
expect(() {
jsObject = jsifyAndAllowInterop({
'frozenObject': frozenNonAnynymousObject,
});
}, returnsNormally, reason: 'should not throw when it encounters a frozen object');
final convertedNestedFrozenObject = getProperty(jsObject, 'frozenObject');
expect(convertedNestedFrozenObject, same(frozenNonAnynymousObject),
reason: 'JS object should have just gotten passed through');
});

test('array object constructed in JS', () {
// Create this and freeze it on the JS side so that we're not starting out with Dart's list implementation
// and potentially any wrapper classes.
final frozenArray = createArray();
expect(_getPrototypeOf(frozenArray), _arrayPrototype, reason: 'test setup check; should be an array');

_freeze(frozenArray);
expect(_isFrozen(frozenArray), isTrue, reason: 'test setup check; should have frozen');

dynamic jsObject;
expect(() {
jsObject = jsifyAndAllowInterop({
'frozenArray': frozenArray,
});
}, returnsNormally, reason: 'should not throw when it encounters a frozen object');
final convertedNestedFrozenObject = getProperty(jsObject, 'frozenArray');
// Note that we don't expect it to be `same(frozenArray)` since the array passes the `is Iterable` check,
// and thus gets processed in _convertDataTree.
expect(convertedNestedFrozenObject, equals(frozenArray),
reason: 'JS object should have been converted properly passed through');
});
});
});
}

@JS('Object.prototype')
external dynamic get _objectPrototype;

@JS('Array.prototype')
external dynamic get _arrayPrototype;

@JS('Object.freeze')
external void _freeze(Object object);

@JS('Object.isFrozen')
external bool _isFrozen(Object object);

@JS('Object.getPrototypeOf')
external dynamic _getPrototypeOf(Object object);
4 changes: 4 additions & 0 deletions test/react_client/js_interop_helpers_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
else
return false;
}

function createArray() {
return [1, 2, 3];
}
</script>

<link rel="x-dart-test" href="js_interop_helpers_test.dart">
Expand Down

0 comments on commit d609e0c

Please sign in to comment.