Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
blob: acde0f070c5e0f66c930f085fc8409c963b9369c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const { Builder, By } = require('selenium-webdriver'),
  chrome = require('selenium-webdriver/chrome.js'),
  chromeOptions = new chrome.Options(),
  { readFile, readdir } = require('node:fs/promises');

chromeOptions.addArguments('--headless', '--disable-gpu', '--no-sandbox');

const buildPath = 'build/assets/images';
const defaultScenario = 'scenario-side_show';
const fixtureFilePath = './test/integration/fixtures/scenario.svg';

let driver, fixture, scenario;

beforeAll(async () => {
  const dirread = readdir(buildPath);
  const fileRead = readFile(fixtureFilePath, 'utf8');
  const filenames = await dirread;
  scenario = filenames.find(filename => filename.includes(defaultScenario));
  fixture = await fileRead;
});

beforeEach(async () => {
  driver = new Builder().forBrowser('chrome').setChromeOptions(chromeOptions).build();
});

describe('a trooper', () => {
  beforeEach(async () => {
    await mockResponse(driver, `/assets/images/${scenario}`, fixture, (document) => {
      placeCounter(document, createTroopCounter(), { x: 1, y: 1 });
      return `<?xml version="1.0" standalone="yes"?>\n` + document.querySelector('svg').outerHTML;
    });
  });

  it('is selected by clicking on its counter', async () => {
    await driver.get(url('/'));
    await page.waitUntilMapLoaded(driver);

    const record = page.findTroopRecordEl(driver);
    const mapResource = page.findMapResourceEl(driver);

    await page.expectNotSelected(await record);
    const counter = await page.findElInFrame(await mapResource, By.css(page.troopCounterSelector()));
    await page.expectNotSelected(counter);

    await counter.click();

    await page.expectSelected(counter);
    await driver.switchTo().defaultContent();
    await page.expectSelected(await record);
  });

  it('is selected by clicking on its record', async () => {
    await driver.get(url('/'));
    await page.waitUntilMapLoaded(driver);

    const record = page.findTroopRecordEl(driver);
    const mapResource = page.findMapResourceEl(driver);

    await page.expectNotSelected(await record);
    const counter = await page.findElInFrame(await mapResource, By.css(page.troopCounterSelector()));
    await page.expectNotSelected(counter);

    await driver.switchTo().defaultContent();
    await record.click();

    await page.expectSelected(await record);
    await driver.switchTo().frame(await mapResource);
    await page.expectSelected(counter);
  });
});

describe('a selected trooper', () => {
  beforeEach(async () => {
    await mockResponse(driver, `/assets/images/${scenario}`, fixture, (document) => {
      placeCounter(document, selectCounter(createTroopCounter()), { x: 1, y: 1 });
      return `<?xml version="1.0" standalone="yes"?>\n` + document.querySelector('svg').outerHTML;
    });
  });

  it('is deselected by clicking on its counter', async () => {
    await driver.get(url('/'));
    await page.waitUntilMapLoaded(driver);

    const record = page.findTroopRecordEl(driver);
    const mapResource = page.findMapResourceEl(driver);

    await page.expectSelected(await record);
    const counter = await page.findElInFrame(await mapResource, By.css(page.troopCounterSelector()));
    await page.expectSelected(counter);

    await counter.click();

    await page.expectNotSelected(counter);
    await driver.switchTo().defaultContent();
    await page.expectNotSelected(await record);
  });

  it('is deselected by clicking on its record', async () => {
    await driver.get(url('/'));
    await page.waitUntilMapLoaded(driver);

    const record = page.findTroopRecordEl(driver);
    const mapResource = page.findMapResourceEl(driver);

    await page.expectSelected(await record);
    const counter = await page.findElInFrame(await mapResource, By.css(page.troopCounterSelector()));
    await page.expectSelected(counter);

    await driver.switchTo().defaultContent();
    await record.click();

    await page.expectNotSelected(await record);
    await driver.switchTo().frame(await mapResource);
    await page.expectNotSelected(counter);
  });
});

afterEach(async () => {
  await driver.quit();
});