Skip to content

Commit

Permalink
add additional test cases for content as data script setup blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 committed Dec 22, 2024
1 parent 4ff2b28 commit 0ecb6f6
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/data/client.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { filterContentByCollection, filterContentByRoute } from '@greenwood/cli/src/lib/content-utils.js';

const CONTENT_STATE = globalThis.__CONTENT_AS_DATA_STATE__ ?? false; // eslint-disable-line no-underscore-dangle
const PRERENDER = globalThis.__CONTENT_OPTIONS__?.PRERENDER ?? false; // eslint-disable-line no-underscore-dangle
const PRERENDER = globalThis.__CONTENT_OPTIONS__?.PRERENDER === 'true'; // eslint-disable-line no-underscore-dangle
const PORT = globalThis?.__CONTENT_OPTIONS__?.PORT ?? 1984; // eslint-disable-line no-underscore-dangle
const BASE_PATH = globalThis?.__GWD_BASE_PATH__ ?? ''; // eslint-disable-line no-underscore-dangle

Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/plugins/resource/plugin-active-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ class ContentAsDataResource extends ResourceInterface {

newBody = newBody.replace('<head>', `
<head>
<script id="active-content-data">
<script id="data-client-options">
globalThis.__CONTENT_OPTIONS__ = globalThis.__CONTENT_OPTIONS__
? globalThis.__CONTENT_OPTIONS__
: {
PORT: ${devServer.port},
PRERENDER: ${this.compilation.config.prerender},
PRERENDER: "${this.compilation.config.prerender}",
}
</script>
`);
Expand Down Expand Up @@ -129,7 +129,7 @@ class ContentAsDataResource extends ResourceInterface {

body = body.replace('<head>', `
<head>
<script id="content-state">
<script id="content-as-data-state">
globalThis.__CONTENT_AS_DATA_STATE__ = true;
</script>
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,28 @@ describe('Build Greenwood With: ', function() {
});
});

describe('<script> tag setup for active content', function() {
let stateScripts;
let optionsScript;

before(function() {
stateScripts = dom.window.document.querySelectorAll('script#content-as-data-state');
optionsScript = dom.window.document.querySelectorAll('script#data-client-options');
});

it('should have a <script> tag that confirms content as data is set', function() {
expect(stateScripts.length).to.equal(1);
expect(stateScripts[0].textContent).to.contain('globalThis.__CONTENT_AS_DATA_STATE__ = true;');
});

it('should have a <script> tag that captures content as data related options', function() {
expect(optionsScript.length).to.equal(1);

expect(optionsScript[0].textContent).to.contain('PORT:1984');
expect(optionsScript[0].textContent).to.contain('PRERENDER:"true"');
});
});

describe('navigation links from getContentByCollection', function() {
let navLinks;

Expand Down
3 changes: 2 additions & 1 deletion packages/cli/test/cases/serve.default/greenwood.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export default {
'/posts': 'https://jsonplaceholder.typicode.com'
}
},
port: 8181
port: 8181,
activeContent: true // just here to test some of the setup <script> output
};
28 changes: 27 additions & 1 deletion packages/cli/test/cases/serve.default/serve.default.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
* '/post': 'https://jsonplaceholder.typicode.com'
* }
* },
* port: 8181
* port: 8181,
* activeContent: true // just here to test some basic content as data defaults
* }
*
* User Workspace
Expand All @@ -33,6 +34,7 @@
* webcomponents.svg
*/
import chai from 'chai';
import { JSDOM } from 'jsdom';
import path from 'path';
import { getOutputTeardownFiles } from '../../../../../test/utils.js';
import { runSmokeTest } from '../../../../../test/smoke-test.js';
Expand Down Expand Up @@ -72,6 +74,30 @@ describe('Serve Greenwood With: ', function() {

runSmokeTest(['serve'], LABEL);

describe('<script> tag setup for active content', function() {
let dom;

before(async function() {
const response = await fetch(`${hostname}`);
dom = new JSDOM(await response.text());
});

it('should have a <script> tag that confirms content as data is set', function() {
const stateScripts = dom.window.document.querySelectorAll('script#content-as-data-state');

expect(stateScripts.length).to.equal(1);
expect(stateScripts[0].textContent).to.contain('globalThis.__CONTENT_AS_DATA_STATE__ = true;');
});

it('should have a <script> tag that captures content as data related options', function() {
const optionsScript = dom.window.document.querySelectorAll('script#data-client-options');

expect(optionsScript.length).to.equal(1);
expect(optionsScript[0].textContent).to.contain('PORT:1984');
expect(optionsScript[0].textContent).to.contain('PRERENDER:"false"');
});
});

// proxies to https://jsonplaceholder.typicode.com/posts via greenwood.config.js
describe('Serve command with dev proxy', function() {
let response = {};
Expand Down

0 comments on commit 0ecb6f6

Please sign in to comment.