emilepharand/Babilonia

View on GitHub
tests/e2e/specs/practicing/context.cy.ts

Summary

Maintainability
A
0 mins
Test Coverage
import {ExpressionForAdding} from '../../../../server/model/ideas/expression';
import {IdeaForAdding} from '../../../../server/model/ideas/ideaForAdding';
import {
    addLanguages, apiUrl, cyRequestPost, cyRequestPut,
} from '../../cy-utils';
import {
    assertIsTyped,
    assertNothingIsTyped,
    assertRowMatchIsFullMatch,
    assertRowMatchIsPartialMatch,
    getResetButton,
    getRowHintButton,
    getRowShowButton,
    typeInRow,
    waitForTableToLoad,
} from './utils';

context('Context', () => {
    specify('Context', () => {
        const languages = addLanguages();
        const e1: ExpressionForAdding = {languageId: 1, text: '(bien le) bonjour (à vous)'};
        const e2: ExpressionForAdding = {languageId: 1, text: 'salut (mon) cher'};
        const e3: ExpressionForAdding = {languageId: 1, text: 'salut (mon) (bel) ami'};
        const e4: ExpressionForAdding = {languageId: 2, text: 'hello'};
        const e5: ExpressionForAdding = {languageId: 3, text: 'buenos días'};
        const e6: ExpressionForAdding = {languageId: 4, text: 'buongiorno'};
        const e7: ExpressionForAdding = {languageId: 5, text: 'guten Tag'};
        const i1: IdeaForAdding = {ee: [e1, e2, e3, e4, e5, e6, e7]};
        cyRequestPost(`${apiUrl}/ideas`, i1);

        // Make some languages practiceable
        languages.forEach(l => {
            l.isPractice = true;
        });
        languages[1].isPractice = false;
        cyRequestPut(`${apiUrl}/languages`, languages);

        cy.get('#practice-link').click();

        waitForTableToLoad(7);

        // Context at beginning of expression is shown
        const row1 = new PracticeRow(1);
        row1.assertIsTyped('(bien le) ');
        row1.assertRowMatchIsPartialMatch();

        // Start to type
        row1.typeInRow('b');
        row1.assertIsTyped('(bien le) b');

        // Complete
        row1.assertRowMatchIsPartialMatch();
        row1.typeInRow('onjour');
        row1.assertRowMatchIsFullMatch('(bien le) bonjour (à vous)');

        // Context appears
        const row2 = new PracticeRow(2);
        row2.assertNothingIsTyped();
        row2.typeInRow('salu');
        row2.assertIsTyped('salu');
        row2.typeInRow('t');
        row2.assertIsTyped('salut (mon) ');
        row2.assertRowMatchIsPartialMatch();

        // Don't have to type space
        row2.typeInRow('c');
        row2.assertIsTyped('salut (mon) c');
        row2.assertRowMatchIsPartialMatch();

        // Complete
        row2.assertRowMatchIsPartialMatch();
        row2.typeInRow('her');
        row2.assertRowMatchIsFullMatch('salut (mon) cher');

        // Two contexts in a row
        const row3 = new PracticeRow(3);
        row3.assertNothingIsTyped();
        row3.typeInRow('salut');
        row3.assertIsTyped('salut (mon) (bel) ');
        row3.assertRowMatchIsPartialMatch();
        row3.typeInRow('ami');
        row3.assertRowMatchIsFullMatch('salut (mon) (bel) ami');

        getResetButton().click();

        // Hinting
        row1.hint();
        row1.assertIsTyped('(bien le) b');
        row1.assertRowMatchIsPartialMatch();
        row1.typeInRow('onjou');
        row1.assertIsTyped('(bien le) bonjou');
        row1.assertRowMatchIsPartialMatch();
        row1.hint();
        row1.assertRowMatchIsFullMatch('(bien le) bonjour (à vous)');

        row2.typeInRow('salu');
        row2.hint();
        row2.assertIsTyped('salut (mon) ');
        row2.hint();
        row2.assertIsTyped('salut (mon) c');
        row2.show();
        row2.assertRowMatchIsFullMatch('salut (mon) cher');

        row3.typeInRow('salu');
        row3.hint();
        row3.assertIsTyped('salut (mon) (bel) ');
        row3.hint();
        row3.assertIsTyped('salut (mon) (bel) a');
        row3.show();
        row3.assertRowMatchIsFullMatch('salut (mon) (bel) ami');
    });

    specify('Context without space before or after it', () => {
        const languages = addLanguages();
        const e1: ExpressionForAdding = {languageId: 1, text: 'pourquoi(?)'};
        const e2: ExpressionForAdding = {languageId: 1, text: 'mais(,) pourquoi(?)'};
        const e3: ExpressionForAdding = {languageId: 2, text: 'why(?)'};
        const e4: ExpressionForAdding = {languageId: 3, text: '(¿)porqué(?)'};
        const i1: IdeaForAdding = {ee: [e1, e2, e3, e4]};
        cyRequestPost(`${apiUrl}/ideas`, i1);

        // Make some languages practiceable
        languages.forEach(l => {
            l.isPractice = true;
        });
        languages[1].isPractice = false;
        cyRequestPut(`${apiUrl}/languages`, languages);

        cy.get('#practice-link').click();

        waitForTableToLoad(4);

        const row1 = new PracticeRow(1);
        row1.assertNothingIsTyped();
        row1.typeInRow('pourquo');
        row1.assertIsTyped('pourquo');
        row1.assertRowMatchIsPartialMatch();
        row1.typeInRow('i');
        row1.assertRowMatchIsFullMatch('pourquoi(?)');

        const row2 = new PracticeRow(2);
        row2.assertNothingIsTyped();
        row2.typeInRow('mai');
        row2.assertIsTyped('mai');
        row2.typeInRow('s');
        row2.assertIsTyped('mais(,) ');
        row2.assertRowMatchIsPartialMatch();
        row2.typeInRow('pourquo');
        row2.assertIsTyped('mais(,) pourquo');
        row2.assertRowMatchIsPartialMatch();
        row2.typeInRow('i');
        row2.assertRowMatchIsFullMatch('mais(,) pourquoi(?)');

        const row3 = new PracticeRow(3);
        row3.assertIsTyped('(¿)');
        row3.assertRowMatchIsPartialMatch();
        row3.typeInRow('porqu');
        row3.assertIsTyped('(¿)porqu');
        row3.assertRowMatchIsPartialMatch();
        row3.typeInRow('é');
        row3.assertRowMatchIsFullMatch('(¿)porqué(?)');

        getResetButton().click();

        row1.typeInRow('pourquo');
        row1.hint();
        row1.assertRowMatchIsFullMatch('pourquoi(?)');

        row2.typeInRow('mai');
        row2.hint();
        row2.assertIsTyped('mais(,) ');
        row2.assertRowMatchIsPartialMatch();
        row2.typeInRow('pourquo');
        row2.hint();
        row2.assertRowMatchIsFullMatch('mais(,) pourquoi(?)');

        row3.typeInRow('porqu');
        row3.hint();
        row3.assertRowMatchIsFullMatch('(¿)porqué(?)');
    });
});

class PracticeRow {
    rowNumber: number;

    constructor(rowNumber: number) {
        this.rowNumber = rowNumber;
    }

    typeInRow(text: string) {
        typeInRow(this.rowNumber, text);
    }

    hint() {
        getRowHintButton(this.rowNumber).click();
    }

    assertIsTyped(expectedText: string) {
        assertIsTyped(this.rowNumber, expectedText);
    }

    assertNothingIsTyped() {
        assertNothingIsTyped(this.rowNumber);
    }

    show() {
        getRowShowButton(this.rowNumber).click();
    }

    assertRowMatchIsFullMatch(expectedText: string) {
        assertRowMatchIsFullMatch(this.rowNumber, expectedText);
    }

    assertRowMatchIsPartialMatch() {
        assertRowMatchIsPartialMatch(this.rowNumber);
    }
}