Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CPLAT-7471 Refactor Prop Type Tests #433

Merged
merged 10 commits into from
Dec 12, 2019
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ before_script:
- pub run build_runner build --delete-conflicting-outputs
- git diff --exit-code
- pub run dart_dev analyze
- pub run dependency_validator --no-fatal-pins -i analyzer,build_runner,build_web_compilers,built_value_generator
- pub run dependency_validator --no-fatal-pins -i analyzer,build_runner,build_web_compilers,built_value_generator,front_end

script:
- pub run dart_dev test -P vm
Expand Down
5 changes: 4 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ dev_dependencies:
dart2_constant: ^1.0.0
dart_dev: ^3.0.0
dependency_validator: ^1.4.0
# front_end is necessary because of a change in build_vm_compilers starting with version 1.0.3, and it should be
# removed when build_vm_compilers no longer requires it to be specified
front_end: ^0.1.27
aaronlademann-wf marked this conversation as resolved.
Show resolved Hide resolved
glob: ^1.2.0
mockito: ^4.1.1
over_react_test: ^2.5.2
over_react_test: ^2.7.0
pedantic: ^1.8.0
test: ^1.9.1
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import 'dart:js';

import 'package:over_react/over_react.dart';
import 'package:react/react_client/react_interop.dart' show PropTypes;
import 'package:over_react_test/over_react_test.dart';
import 'package:test/test.dart';

import '../../../../test_util/test_util.dart';
Expand All @@ -23,137 +22,117 @@ part 'constant_required_accessor_integration_test.over_react.g.dart';

void main() {
group('(Component2) propTypes properly identifies required props by', () {
List<String> consoleErrors;
JsFunction originalConsoleError;

setUp(() {
// PropTypes by default will only throw a specific error one time per Component Class.
// This resets the cache after each test so it throws again.
// See: https://www.npmjs.com/package/prop-types#proptypesresetwarningcache
PropTypes.resetWarningCache();

originalConsoleError = context['console']['error'];
consoleErrors = [];
context['console']['error'] = JsFunction.withThis((self, message) {
consoleErrors.add(message);
originalConsoleError.apply([message], thisArg: self);
});
});

tearDown(() {
context['console']['error'] = originalConsoleError;
});
group('throwing when a prop is required and not set', () {

test('on mount', () {
mount(
(ComponentTest()
..nullable = true
..requiredAndLengthLimited = [1,2]
)(),
attachedToDocument: true,
);

expect(consoleErrors, isNotEmpty, reason: 'should have outputted a warning');
expect(consoleErrors, [contains('ComponentTestProps.required')]);
expect(() {
mount(
(ComponentTest()
..nullable = true
..requiredAndLengthLimited = [1,2]
)(),
attachedToDocument: true,
);
}, logsPropRequiredError('ComponentTestProps.required'));
});

test('on re-render', () {
var jacket = mount((ComponentTest()
..required = true
..nullable = true
..requiredAndLengthLimited = [1,2]
)(),
attachedToDocument: true,
);

expect(consoleErrors, isEmpty, reason: 'should not have outputted a warning but found: $consoleErrors');

jacket.rerender((ComponentTest()
TestJacket jacket;

expect(() {
jacket = mount(
(ComponentTest()
..required = true
..nullable = true
..requiredAndLengthLimited = [1,2]
)(),
attachedToDocument: true);
}, logsNoPropTypeWarnings);

expect(() {
jacket.rerender((ComponentTest()
..nullable = true
..requiredAndLengthLimited = [1,2]
)()
);

expect(consoleErrors, isNotEmpty, reason: 'should have outputted a warning');
expect(consoleErrors, [contains('ComponentTestProps.required')]);
);
}, logsPropRequiredError('ComponentTestProps.required'));
});

});

group('throwing when a prop is required and set to null', () {
test('on mount', () {
render((ComponentTest()
expect(() {
mount((ComponentTest()
..required = null
..nullable = true
..requiredAndLengthLimited = [1,2]
)());

expect(consoleErrors, isNotEmpty, reason: 'should have outputted a warning');
expect(consoleErrors, [contains('ComponentTestProps.required')]);
}, logsPropRequiredError('ComponentTestProps.required'));
});

test('on re-render', () {
var jacket = mount((ComponentTest()
..required = true
TestJacket jacket;

expect(() {
jacket = mount(
(ComponentTest()
..required = true
..nullable = true
..requiredAndLengthLimited = [1,2]
)(),
attachedToDocument: true);
}, logsNoPropTypeWarnings);

expect(() {
jacket.rerender((ComponentTest()
..required = null
..nullable = true
..requiredAndLengthLimited = [1,2]
)(),
attachedToDocument: true,
);

expect(consoleErrors, isEmpty, reason: 'should not have outputted a warning but found: $consoleErrors');

jacket.rerender((ComponentTest()
..required = null
..nullable = true
..requiredAndLengthLimited = [1,2]
)());

expect(consoleErrors, isNotEmpty, reason: 'should have outputted a warning');
expect(consoleErrors, [contains('ComponentTestProps.required')]);
)());
}, logsPropRequiredError('ComponentTestProps.required'));
});
});

group('throwing when a prop is nullable and not set', () {
test('on mount', () {
render((ComponentTest()
..required = true
..requiredAndLengthLimited = [1,2]
)());
expect(consoleErrors, isNotEmpty, reason: 'should have outputted a warning');
expect(consoleErrors, [contains('ComponentTestProps.nullable')]);
expect(() {
mount((ComponentTest()
..required = true
..requiredAndLengthLimited = [1,2]
)());
}, logsPropError('ComponentTestProps.nullable'));
});

test('on re-render', () {
var jacket = mount((ComponentTest()
TestJacket jacket;

expect(() {
jacket = mount(
(ComponentTest()
..required = true
..nullable = true
..requiredAndLengthLimited = [1,2]
)(),
attachedToDocument: true);
}, logsNoPropTypeWarnings);

expect(() {
jacket.rerender((ComponentTest()
..required = true
..nullable = true
..requiredAndLengthLimited = [1,2]
)(),
attachedToDocument: true,
);

expect(consoleErrors, isEmpty, reason: 'should not have outputted a warning but found: $consoleErrors');

jacket.rerender((ComponentTest()
..required = true
..requiredAndLengthLimited = [1,2]
)());

expect(consoleErrors, isNotEmpty, reason: 'should have outputted a warning');
expect(consoleErrors, [contains('ComponentTestProps.nullable')]);
)());
}, logsPropError('ComponentTestProps.nullable'));
});
});

group('not throwing when a prop is required and set', () {
test('on mount', () {
render((ComponentTest()
..nullable = true
..required = true
..requiredAndLengthLimited = [1,2]
)());
expect(consoleErrors, isEmpty, reason: 'should not have outputted a warning but found: $consoleErrors');
expect(() {
mount((ComponentTest()
..nullable = true
..required = true
..requiredAndLengthLimited = [1,2]
)());
}, logsNoPropTypeWarnings);
});

test('on re-render', () {
Expand All @@ -165,69 +144,68 @@ void main() {
attachedToDocument: true,
);

expect(consoleErrors, isEmpty, reason: 'should not have outputted a warning but found: $consoleErrors');

jacket.rerender((ComponentTest()
..required = true
..nullable = true
..requiredAndLengthLimited = [1,2]
)());

expect(consoleErrors, isEmpty, reason: 'should not have outputted a warning but found: $consoleErrors');
expect(() {
jacket.rerender((ComponentTest()
..required = true
..nullable = true
..requiredAndLengthLimited = [1,2]
)());
}, logsNoPropTypeWarnings);
});
});

group('not throwing when a prop is nullable and set to null', () {
test('on mount', () {
render((ComponentTest()
..nullable = null
..requiredAndLengthLimited = [1,2]
..required = true
)());
expect(consoleErrors, isEmpty, reason: 'should not have outputted a warning but found: $consoleErrors');
expect(() {
mount((ComponentTest()
..nullable = null
..requiredAndLengthLimited = [1,2]
..required = true
)());
}, logsNoPropTypeWarnings);
});

test('on re-render', () {
var jacket = mount((ComponentTest()
TestJacket jacket;

expect(() {
jacket = mount(
(ComponentTest()
..required = true
..nullable = true
..requiredAndLengthLimited = [1,2]
)(),
attachedToDocument: true);
}, logsNoPropTypeWarnings);

expect(() {
jacket.rerender((ComponentTest()
..required = true
..nullable = true
..nullable = null
..requiredAndLengthLimited = [1,2]
)(),
attachedToDocument: true,
);

expect(consoleErrors, isEmpty, reason: 'should not have outputted a warning but found: $consoleErrors');

jacket.rerender((ComponentTest()
..required = true
..nullable = null
..requiredAndLengthLimited = [1,2]
)());

expect(consoleErrors, isEmpty, reason: 'should not have outputted a warning but found: $consoleErrors');
)());
}, logsNoPropTypeWarnings);
});
});

group('when a consumer propType function is also provided', () {
test('required fires', () {
render((ComponentTest()
expect(() {
mount((ComponentTest()
..nullable = null
..required = true
)());

expect(consoleErrors, isNotEmpty, reason: 'should have outputted a warning');
expect(consoleErrors, [contains('ComponentTestProps.requiredAndLengthLimited')]);
}, logsPropValueError('null', 'ComponentTestProps.requiredAndLengthLimited'));
});

test('consumer check fires', () {
render((ComponentTest()
expect(() {
mount((ComponentTest()
..required = true
..nullable = true
..requiredAndLengthLimited = [1]
)());

expect(consoleErrors, isNotEmpty, reason: 'should have outputted a warning');
expect(consoleErrors, [contains('must have a length of 2')]);
}, logsPropValueError('1', 'ComponentTestProps.requiredAndLengthLimited'));
});
});
});
Expand Down Expand Up @@ -264,4 +242,3 @@ class ComponentTestComponent extends UiComponent2<ComponentTestProps> {
@override
render() => Dom.div()();
}

Loading