Skip to content

Commit

Permalink
update graph tests to focus less on implementation details
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 committed Feb 27, 2020
1 parent 6648e39 commit c81db68
Showing 1 changed file with 8 additions and 56 deletions.
64 changes: 8 additions & 56 deletions packages/cli/test/cases/build.data.graph/build.data.graph.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const TestBed = require('../../../../../test/test-bed');

describe('Build Greenwood With: ', function() {
const LABEL = 'Data from GraphQL';
const apolloStateLabel = 'window.__APOLLO_STATE__=';
const apolloStateRegex = /window.__APOLLO_STATE__=({.*?});/;
let setup;

before(async function() {
Expand Down Expand Up @@ -73,25 +73,15 @@ describe('Build Greenwood With: ', function() {
expect(await glob.promise(path.join(this.context.publicDir, './cache.json'))).to.have.lengthOf(1);
});

it('should output one cache.json file with expected cache contents for ROOT_QUERY', function() {
const cacheContents = require(path.join(this.context.publicDir, './cache.json'));

expect(cacheContents).to.contain.key('ROOT_QUERY.navigation.0');
expect(cacheContents).to.contain.key('ROOT_QUERY');
});

it('should have one window.__APOLLO_STATE__ <script> with expected QUERY keys', () => {
it('should have one window.__APOLLO_STATE__ <script> with (approximated) expected state', () => {
const scriptTags = dom.window.document.querySelectorAll('head > script');
const apolloScriptTags = Array.prototype.slice.call(scriptTags).filter(script => {
return script.getAttribute('data-state') === 'apollo';
});
const innerHTML = apolloScriptTags[0].innerHTML;
const json = JSON.parse(innerHTML.replace(apolloStateLabel, '').replace(';', ''));

expect(json).to.contain.key('ROOT_QUERY.navigation.0');
expect(json).to.contain.key('ROOT_QUERY');

expect(innerHTML).to.contain(apolloStateLabel);
expect(apolloScriptTags.length).to.equal(1);
expect(innerHTML).to.match(apolloStateRegex);
});

it('should have a <header> tag in the <body>', function() {
Expand Down Expand Up @@ -124,58 +114,20 @@ describe('Build Greenwood With: ', function() {
expect(await glob.promise(path.join(this.context.publicDir, 'blog', 'cache.json'))).to.have.lengthOf(1);
});

it('should output one cache.json file with expected cache contents for ROOT_QUERY', function() {
const cacheContents = require(path.join(this.context.publicDir, 'blog', 'cache.json'));

expect(cacheContents).to.contain.key('ROOT_QUERY.navigation.0');
expect(cacheContents).to.contain.key('ROOT_QUERY');
});

it('should output one cache.json file with expected cache length for Pages', function() {
it('should output one cache.json file to be defined', function() {
const cacheContents = require(path.join(this.context.publicDir, 'blog', 'cache.json'));
const pages = Object.keys(cacheContents).filter((key) => {
return key.includes('Page:');
});

expect(pages.length).to.equal(2);
expect(cacheContents).to.not.be.undefined;
});

it('should have one window.__APOLLO_STATE__ <script> tag set in index.html', () => {
it('should have one window.__APOLLO_STATE__ <script> with (approximated) expected state', () => {
const scriptTags = dom.window.document.querySelectorAll('head > script');
const apolloScriptTags = Array.prototype.slice.call(scriptTags).filter(script => {
return script.getAttribute('data-state') === 'apollo';
});

expect(apolloScriptTags.length).to.be.equal(1);
expect(apolloScriptTags[0].innerHTML).to.contain('window.__APOLLO_STATE__={');
});

it('should have one window.__APOLLO_STATE__ <script> with expected QUERY keys', () => {
const scriptTags = dom.window.document.querySelectorAll('head > script');
const apolloScriptTags = Array.prototype.slice.call(scriptTags).filter(script => {
return script.getAttribute('data-state') === 'apollo';
});
const innerHTML = apolloScriptTags[0].innerHTML;
const json = JSON.parse(innerHTML.replace(apolloStateLabel, '').replace(';', ''));

expect(json).to.contain.key('ROOT_QUERY.navigation.0');
expect(json).to.contain.key('ROOT_QUERY');

expect(innerHTML).to.contain(apolloStateLabel);
});

it('should have one window.__APOLLO_STATE__ <script> with expected Pages: keys', function() {
const scriptTags = dom.window.document.querySelectorAll('head > script');
const apolloScriptTags = Array.prototype.slice.call(scriptTags).filter(script => {
return script.getAttribute('data-state') === 'apollo';
});
const innerHTML = apolloScriptTags[0].innerHTML;
const json = JSON.parse(innerHTML.replace(apolloStateLabel, '').replace(';', ''));
const pages = Object.keys(json).filter((key) => {
return key.includes('Page:');
});

expect(pages.length).to.equal(2);
expect(apolloScriptTags[0].innerHTML).to.match(apolloStateRegex);
});

it('should have a <header> tag in the <body>', function() {
Expand Down

0 comments on commit c81db68

Please sign in to comment.