dimanche 11 novembre 2018

How do I make test for method of component in Angular 5?

I have a component in Angular 5 with simple logic: user have to press a key, the method onKeyDown calculates result value, so in 1st press resault should be 4, in 2th should be 8:

@Component({
    selector: 'app-landing',
    templateUrl: './main.component.html',
    styleUrls: ['./main.component.less']
})

export class MainComponent {
    result = 0;
    cycle = 0;

    constructor() {
        document.addEventListener('keydown', (e) => this.onKeyDown(e), false);
    }

    onKeyDown(e: any) {
        // do something
        sont someConst = 4;
        result = result + someConst;
        cycle++;
        return {
            cycle: cycle,
            result: result
        };
    }
}

Then I whant to do test for this process: I want to simulate the users pressing button and check if results are what I expect:

// pseudo code
DoTest {
    MainComponent instanceForTest = new MainComponent();
    var test_01 = instanceForTest.onKeyDown(13);
    var test_02 = instanceForTest.onKeyDown(13);
    if (
        test_01.wcycle == 1 &&
        test_01.result == 4 
    ){
        Console.WriteLine("Test 01 passed");
    }
    if (
        test_02.wcycle == 2 &&
        test_02.result == 8 
    ){
        Console.WriteLine("Test 02 passed");
    }
}

How do I make this test in ngx?

Aucun commentaire:

Enregistrer un commentaire