ngUpgraders / ng-forward

The default solution for those that want to write Angular 2.x style code in Angular 1.x
410 stars 36 forks source link

Error in docs about testing using TestComponentBuilder? #136

Closed jasonmacdonald closed 8 years ago

jasonmacdonald commented 8 years ago

I think there might be an error in the docs about testing. It says to import TestComponentBuilder from 'ng-forward', but that returns a "cannot resolve symbol' error. I checked and the index.d.ts does not import that file. It seems I have to import it from 'ng-forward/cjs/testing'. Is this correct, or did I do something wrong?

Here's a quick example. The first import gives an error, the second one doesn't.

import {Component, TestComponentBuilder} from 'ng-forward';
import {TestComponentBuilder} from 'ng-forward/cjs/testing';
import {Home} from './home';

// Instantiate the Builder, this part is different than ng2.
// In ng2 you inject tcb.
const tcb = new TestComponentBuilder();

// Create your test bed
@Component({ selector: 'test-cmp')
class Test {}

describe('Home', () => {
    it('should have name', () => {
        let html = '<home></home>';

        // Use tcb to override templates and providers, then create your fixture.
        tcb
            .overrideTemplate(Test, html)
            .createAsync(Test).then(fixture => {
            let homeComp = fixture.componentInstance;
            expect(homeComp.name).toEqual('Home');
        });
    });
})
timkindberg commented 8 years ago

You are right! I'll fix it. You might be able to import from 'ng-forward/testing' as well.

jasonmacdonald commented 8 years ago

Damn, you're fast. I was still editing my post lol.

And no, using 'ng-forward/testing' gives the same error, "cannot find symbol'.

jasonmacdonald commented 8 years ago

Found another error in the docs. :( Inside the createAsync callback, you must call done(); or Jasmine will always return "true" for the expectation. Also, need to add variable "done" to the "it" call to gain access to the done method...

it('should have name', (done) => {
        let html = '<home></home>';

        // Use tcb to override templates and providers, then create your fixture.
        tcb
            .overrideTemplate(Test, html)
            .createAsync(Test).then(fixture => {
            let homeComp = fixture.componentInstance;
            expect(homeComp.name).toEqual('Home');
            done();
        });
    });

I know the Docs say this isn't an actual async call, but Jasmine seems to be treating it as one. Perhaps the Promise is enough for it to escape the event loop? Either way, probably best to treat it like one so when you switch to Angular2 (and it becomes async) the test will be ready. :)

timkindberg commented 8 years ago

Yup you are right.