Skip to content

Commit

Permalink
implemented tests 1.2, 3.1, 3.2, 3.3 for #56
Browse files Browse the repository at this point in the history
  • Loading branch information
queleok committed Aug 22, 2021
1 parent 857ff8d commit 85173c3
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/scripts/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ class PromiseQueue {
private curr: Node;
private length: number;
private depletion_cb?: () => void;
private time_scale: number;

constructor() {
constructor(time_scale = 1) {
this.curr = { item: Promise.resolve("success") };
this.begin = { item: Promise.resolve("success"), next: this.curr };
this.length = 0;
this.time_scale = time_scale;
}

enqueue(this: PromiseQueue, word: string): Promise<FetchResult> {
Expand All @@ -38,7 +40,7 @@ class PromiseQueue {
else resolve("network-failure");
})
.finally(() => { q.dequeue(); });
}, 500);
}, 500 * q.time_scale);
});
});
this.curr.next = { item: ret };
Expand Down
45 changes: 44 additions & 1 deletion src/scripts/test/e2e/publish-word.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const send_first_n_letters = async (n: number): Promise<string> => {

beforeAll(async () => {
await page.goto("http://localhost:8080/play.html");
await page.exposeFunction("_puppeteerGetSpeedup", () => { return 100; });
});

beforeEach(async () => {
Expand Down Expand Up @@ -114,7 +115,7 @@ test('Confirm failed word has respective class', async () => {
expect(published_first_word).toBe(first_word);
}, timeout);

test('Confirm recoverably failed word has respective class', async () => {
test('Confirm recoverably failed words have respective class, their occurrence yields network issues disclaimer to appear, and its successful resending yields class modification', async () => {
getRequestMock = (word: string | undefined) => {
return {
status: 400,
Expand All @@ -129,4 +130,46 @@ test('Confirm recoverably failed word has respective class', async () => {
const first_word_eh = (await page.waitForSelector('.network-failure'))!;
const published_first_word = (await getPropertyUnsafe(first_word_eh, 'textContent'))!;
expect(published_first_word).toBe(first_word);

const second_word = (await send_first_n_letters(4))!;

const moved_first_word_eh = (await page.waitForSelector('.network-failure ~ .network-failure'))!;
const moved_published_first_word = (await getPropertyUnsafe(moved_first_word_eh, 'textContent'))!;
expect(moved_published_first_word).toBe(first_word);

const second_word_eh = (await page.$('.network-failure'))!;
const published_second_word = (await getPropertyUnsafe(second_word_eh, 'textContent'))!;
expect(published_second_word).toBe(second_word);

const disclaimer_eh = await page.waitForSelector('#network-issues-disclaimer', { visible: true, timeout: 10000 });
expect(disclaimer_eh).toBeDefined();

getRequestMock = (word: string | undefined) => {
return {
status: 200,
headers: { "Access-Control-Allow-Origin": "*" },
contentType: 'application/json',
body: `[{ "word": "${word}", "meanings": [ { "partOfSpeech": "stub", "definitions": [ { "definition": "stub" } ]}]}]`
};
};

const resend_btn_eh = await page.waitForSelector('#resend', { visible: true, timeout: 10000 });
expect(resend_btn_eh).toBeDefined();
await resend_btn_eh!.click();

const validated_word_eh = await page.waitForSelector('.success ~ .success');
expect(validated_word_eh).toBeDefined();
const published_validated_word = (await getPropertyUnsafe(validated_word_eh!, 'textContent'))!;
expect(published_validated_word).toBe(first_word);

const validated_words = await page.$$('.success');
expect(validated_words).toBeDefined();
expect(validated_words.length).toBe(2);

const failed_words = await page.$$('.network-failure');
expect(failed_words).toBeDefined();
expect(failed_words.length).toBe(0);

const disclaimer_box_model = await disclaimer_eh!.boxModel();
expect(disclaimer_box_model).toBeNull();
}, timeout);
2 changes: 0 additions & 2 deletions src/scripts/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ class LetterWidget {
this.sync = synchronizer;

this.letter = letter;
console.log(`create letter ${this.letter}`);

this.container = document.createElement('div');
this.container.setAttribute('class', 'hbox-nowrap cell l-' + letter);
Expand All @@ -237,7 +236,6 @@ class LetterWidget {
release = () => {
this.container.removeEventListener('click', this.toggleHighlighting);
this.dehighlight();
console.log(`destroy letter ${this.letter}`);
}

private toggleHighlighting = (e: Event) => {
Expand Down
12 changes: 9 additions & 3 deletions src/scripts/words-hunter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import { LetterWidget, WordSynchronizer } from './ui.js';

let freqmap = Array(26).fill(0);
let queue = new PromiseQueue();
let time_scale = 1.0;

declare global {
interface Window { _puppeteerGetSpeedup: () => Promise<number>; }
}

function generateLetters(letters_div: HTMLElement, synchronizer: WordSynchronizer) {
letters_div.textContent = '';
Expand Down Expand Up @@ -113,7 +118,7 @@ function startTimer(minutes: number
}
--sec;
left!.textContent = formatTime(sec);
}, 1000);
}, 1000 * time_scale);

return tmr;
}
Expand Down Expand Up @@ -151,8 +156,9 @@ function publishWord(word: string) {
}
}

function reset() {
queue = new PromiseQueue();
async function reset() {
if (window.hasOwnProperty('_puppeteerGetSpeedup')) time_scale = 1 / await window._puppeteerGetSpeedup();
queue = new PromiseQueue(time_scale);

const disclaimer = document.getElementById('network-issues-disclaimer') as HTMLElement;
disclaimer.classList.add('hidden');
Expand Down

0 comments on commit 85173c3

Please sign in to comment.