Bram's Dev Blog

home

100 Days of Code Day 65 - Writing my first DSpace 7 Angular tests

28 Oct 2018

Writing tests for the language switch

How can I only execute tests from a particular file, and not run the tests of the entire project?

To get my own tests to run, I ideally want those tests to run in isolation first, before having to pass through the tests of the entire project all the time.

How to execute only one test spec with angular-cli learned me that the test.ts file can be modified to run only the tests of a specific component. However, this doesn’t seem to apply for the DSpace 7 Angular codebase.

There I found out that in the karma.conf.js, where the Karma test runner is configured, there’s a reference to spec-bundle.js. It looks like that’s indeed the place to limit which tests are included for the run.

DebugElement vs HTMLElement

I understand from Angular.io Testing guidelines that your tests might not always be running on a browser platform. They might be running on something that doesn’t support queryselector, straight on the nativelement provided by the fixture. However, I’m not sure if the whole wrapping and workaround with a DebugElements is required or even desired for DSpace 7 Angular testing.

Typescript colon (:) vs javascript equals sign (=) for declarations

Thanks to TypeScript : Colon vs Equal To ( Angular Tutorial ) and Typescript documentation on Basic Types, I was reminded of the following when I got confused about : vs =.

Basically javascript doesn’t have types, so you could do assignments like:

const a = b;

Typescript adds [: type] as an optional type declaration, so the pattern for declaring variables becomes:

name[: type][ = value];

Const is not about immutability

tl;dr of ES2015 const is not about immutability

Testbed schemas NO_ERRORS_SCHEMA

To ignore child components, unrecognized elements and attributes, you can set up the testbed with the NO_ERRORS_SCHEMA. Source: Angular.io Testing documentation

Intellij IDEA complaining about Observable.of

In many classes that I have not been modifying, I get complaints about calls like:

spyOn(store, 'select').and.returnValue(Observable.of({ navCollapsed: true }));

The complaint is Property ‘of’ does not exist on type ‘typeof Observable’. I couldn’t determine yet whether it’s something wrong with my particular configuration or if something might need to change on those files.

Mocking / Creating stubs from variables read from config?

The language switch components is supposed to read the list of activated languages and their labels from the config. Not sure if it is OK in a test to rely on this behaviour, or whether mock values from config also have to be provided.

toBeTruthy()

I’ve come across this a couple of times and have no idea what it means.

Dealing with the translation service dependency injection

How to unit test a component with TranslateService and the translate pipe? features a whole discussion of what and how to mock for TranslateService, where the developer behind ngx-translate responded by adding a spec to his example app.

This should provide enough examples of how to deal with testing if some parts effectively get translated correctly.

Day 66 Plan

Solve the dependency injection problems for the tests

Future days - DSpace 7 Angular

Future days - Analyzer.atmire.com work

Future Days - Productivity

Future days - Jekyll http://bram-atmire.github.io/ site

Future Days - Atmire.com work

Investigate and work on search engine optimization (SEO) for the main atmire.com website.

Future Days - Learning just for learning

Sustainability challenge - Finish before Christmas

If I continue like October, I could hit day 68 by end of October and day 98 by end of November.