const schema = require('./data/author-list.schema.json');
const data = require('./data/author-list.example.json');
const fs = require('fs');
const path = require('path');
const component = fs.readFileSync(path.resolve(__dirname, 'author-list.hbs'), 'utf8');
const validate = require('jsonschema').validate;
const handlebars = require('handlebars');
const { configureAxe, toHaveNoViolations } = require('jest-axe');
expect.extend(toHaveNoViolations);
const template = handlebars.compile(component);
const rendered = template(data);

const axe = configureAxe({
  rules: {
    region: { enabled: false }
  }
});

test('Example data matches schema', () => {
  const results = validate(data, schema);
  if (results.errors.length) {
    results.errors.forEach(error => {
      console.error(`❌ ${error.property.split('.').slice(1).join('.')} ${error.message}`);
    });
  }
	expect(results.valid).toBe(true);
});

test('Template can be compiled and rendered', () => {
  document.body.innerHTML = rendered;
  const container = document.querySelector('*');
	expect(container).toBeDefined();
});

test('Template passes basic HTML accessibility', async () => {
  expect(await axe(rendered)).toHaveNoViolations();
});

test('README file exists', () => {
  const exists = fs.existsSync(path.resolve(__dirname, 'README.md'));
  expect(exists).toBe(true);
});