import * as Schema from './schema';

const parseCheckSchema = (schema) => {
  const json = JSON.parse(schema);
  expect(Object.prototype.toString.call(schema)).toEqual('[object String]');
  expect(Object.prototype.toString.call(json)).toEqual('[object Object]');
  expect(json['@context']).toEqual('https://schema.org');
  return json;
};

describe('Schema Helper', () => {
  test('schemaGenerator(PCSA)', () => {
    process.env.GATSBY_SITE_NAME = 'playcasino.co.za';
    const output = Schema.schemaGenerator({
      seo_json_schema: 'SEO JSON Schema',
      breadcrumbs: [{ path: '/' }],
      type: 'operator',
      path: '/',
      sections: { main: { modules: [{ name: 'faq' }] } },
      siteSchema: {
        site_name: 'Site Name',
      },
    });

    expect(Object.prototype.toString.call(output)).toEqual('[object Array]');

    expect(Object.prototype.toString.call(output[0])).toEqual('[object String]');
    expect(output[0]).toEqual('SEO JSON Schema');

    const jsonBreadcrumbs = parseCheckSchema(output[1]);
    expect(jsonBreadcrumbs['@type']).toEqual('BreadcrumbList');

    expect(output[2]).toEqual(null);

    const jsonOrganization = parseCheckSchema(output[3]);
    expect(jsonOrganization['@type']).toEqual('Organization');

    const jsonArticle = parseCheckSchema(output[4]);
    expect(jsonArticle['@type']).toEqual('Review');

    const jsonFaq = parseCheckSchema(output[5]);
    expect(jsonFaq['@type']).toEqual('FAQPage');
  });

  test('schemaGenerator(IL)', () => {
    process.env.GATSBY_SITE_NAME = 'irishluck.ie';
    const output = Schema.schemaGenerator({
      seo_json_schema: 'SEO JSON Schema',
      type: 'article',
      path: '/test-path',
    });

    expect(Object.prototype.toString.call(output)).toEqual('[object Array]');

    expect(Object.prototype.toString.call(output[0])).toEqual('[object String]');
    expect(output[0]).toEqual('SEO JSON Schema');

    expect(output[1]).toEqual(null);

    const jsonWebpage = parseCheckSchema(output[2]);
    expect(jsonWebpage['@type']).toEqual('WebPage');
    expect(jsonWebpage.keywords).toEqual(undefined);
    expect(jsonWebpage.author.publishingPrinciples).toEqual(undefined);
    expect(jsonWebpage.author.knowsAbout).toEqual(undefined);

    expect(output[3]).toEqual(null);

    const jsonArticle = parseCheckSchema(output[4]);
    expect(jsonArticle['@type']).toEqual('Article');

    expect(output[5]).toEqual(null);
  });

  test('schemaGenerator(NSA)', () => {
    process.env.GATSBY_SITE_NAME = 'norskespilleautomater.com';
    const output = Schema.schemaGenerator({
      seo_json_schema: 'SEO JSON Schema',
      type: 'other',
      path: '/test-path',
      siteSchema: {
        publishing_principles: 'https://www.norskespilleautomater.com/om-oss/prinsipper',
      },
    });

    expect(Object.prototype.toString.call(output)).toEqual('[object Array]');

    expect(Object.prototype.toString.call(output[0])).toEqual('[object String]');
    expect(output[0]).toEqual('SEO JSON Schema');

    expect(output[1]).toEqual(null);

    const jsonWebpage = parseCheckSchema(output[2]);
    expect(jsonWebpage['@type']).toEqual('WebPage');
    expect(jsonWebpage.keywords).toEqual(undefined);
    expect(jsonWebpage.author.publishingPrinciples).toEqual(
      'https://www.norskespilleautomater.com/om-oss/prinsipper'
    );
    expect(Object.prototype.toString.call(jsonWebpage.author.knowsAbout)).toEqual('[object Array]');
    expect(jsonWebpage.author.knowsAbout).toHaveLength(4);
    expect(Object.prototype.toString.call(jsonWebpage.author.knowsAbout[0])).toEqual(
      '[object Object]'
    );
    expect(jsonWebpage.author.knowsAbout[0]['@type']).toEqual('Thing');
    expect(jsonWebpage.author.knowsAbout[0].name).toEqual('Norway');

    expect(output[3]).toEqual(null);

    expect(output[4]).toEqual(null);

    expect(output[5]).toEqual(null);
  });
});
