I'm writing a polymer app and have a service that is used by polymer elements. I want to test this service but can't figure out how.
Here's what I have in the service:
<script src="../../webcomponentsjs/webcomponents-lite.js"></script>
<script src="../../web-component-tester/browser.js"></script>
<link rel="import" href="../test-service.html">
<script>
define('test-service', () => {
class TestService {
constructor(componentA, componentB) {
this.componentA = componentA;
this.componentB = componentB;
}
}
return TestService;
});
</script>
How can I test this? If I try simply including the .html file I don't have access to TestService.
Finally figured it out. It all had to do with Polymer IMD and dependency injection. Defining a test suite looks different with it:
define('test-service-test', ['test-service'], (TestService) => {
let testServiceInstance = new TestService(1, 2);
test('basic test', function(){
assert.equal(testServiceInstance.componentA, 1);
assert.equal(testServiceInstance.componentB, 2);
})
});
This is the full html of an example test. Basically add your test-service tag via test-fixture and then see if it work appropriately.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes">
<title>test-service test</title>
<script src="../../webcomponentsjs/webcomponents-lite.js"></script>
<script src="../../web-component-tester/browser.js"></script>
<link rel="import" href="../test-service.html">
</head>
<body>
<test-fixture id="BasicTestFixture">
<template>
<test-service></test-service>
</template>
</test-fixture>
<script>
suite('test-service', function() {
test('instantiating the element with default properties works', function() {
let testService = fixture('BasicTestFixture');
assert.equal(testService.apiUrl, 'http://myapi.domain.com');
// possible something like
// let addResult = testService.addElement(...);
// assert.equals(addResult, '{"result": "success"}');
});
});
</script>
</body>
</html>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With