layout.e2e.js 1.56 KB
Newer Older
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
1 2 3
import puppeteer from 'puppeteer';
import RouterConfig from '../../config/router.config';

4 5
const BASE_URL = `http://localhost:${process.env.PORT || 8000}`;

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
function formatter(data) {
  return data
    .reduce((pre, item) => {
      if (item.routes) {
        return pre.concat(formatter(item.routes));
      }
      pre.push(item.path);
      return pre;
    }, [])
    .filter(item => item);
}

describe('Homepage', () => {
  let browser;
  let page;

  const testAllPage = async layout =>
    new Promise(async (resolve, reject) => {
      const loadPage = async index => {
        const path = layout[index];
        try {
27
          await page.goto(`${BASE_URL}${path}`, { waitUntil: 'networkidle2' });
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
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
          const haveFooter = await page.evaluate(
            () => document.getElementsByTagName('footer').length > 0
          );

          expect(haveFooter).toBeTruthy();

          if (index < layout.length - 1) {
            loadPage(index + 1);
          } else {
            resolve('ok');
          }
        } catch (error) {
          reject(error);
        }
      };
      loadPage(0);
    });

  beforeAll(async () => {
    browser = await puppeteer.launch({ args: ['--no-sandbox'] });
    page = await browser.newPage();
    jest.setTimeout(1000000);
  });

  it('test user layout', async () => {
    const userLayout = formatter(RouterConfig[0].routes);
    await testAllPage(userLayout);
  });

  it('test base layout', async () => {
    const baseLayout = formatter(RouterConfig[1].routes);
    await testAllPage(baseLayout);
  });

  afterAll(() => browser.close());
});