Skip to content

Commit

Permalink
Amending browser renderer with exception for root index (#55)
Browse files Browse the repository at this point in the history
* fix: amending browser renderer with exception for root index

* fix: updating specificity of exception check

* test: adding tests for root index.html
  • Loading branch information
hutchgrant authored and thescientist13 committed Apr 27, 2019
1 parent b34dd01 commit 4696b4b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
8 changes: 8 additions & 0 deletions packages/cli/lib/util/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ module.exports = async (url, label, route, outputDirectory) => {
const html = dom.serialize();
const target = path.join(outputDirectory, route);

// Exception for index file in root public directory
const endOfPathFolder = target.substring(target.lastIndexOf('/public/'), target.length);
const isRootPublicDirectoryException = endOfPathFolder === '/public/index';

if (isRootPublicDirectoryException) {
return await fs.writeFileSync(path.join(outputDirectory, 'index.html'), html);
}

await fs.mkdirSync(target, { recursive: true });
return await fs.writeFileSync(path.join(target, 'index.html'), html);
};
38 changes: 32 additions & 6 deletions test/cli.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,44 @@ describe('building greenwood with default context (no user workspace)', () => {
it('should output one JS bundle file', async () => {
expect(await glob.promise(path.join(CONTEXT.publicDir, './index.*.bundle.js'))).to.have.lengthOf(1);
});


describe('default generated index page in public directory', () => {
const indexPageHeading = 'Greenwood';
const indexPageBody = 'This is the home page built by Greenwood. Make your own pages in src/pages/index.js!';
let dom;

beforeEach(async() => {
dom = await JSDOM.fromFile(path.resolve(CONTEXT.publicDir, 'index.html'));
});

it('should output an index.html file within the root public directory', () => {
expect(fs.existsSync(path.join(CONTEXT.publicDir, './index.html'))).to.be.true;
});

it('should have the expected heading text within the index page in the public directory', async() => {
const heading = dom.window.document.querySelector('h3.wc-md-index').textContent;

expect(heading).to.equal(indexPageHeading);
});

it('should have the expected paragraph text within the index page in the public directory', async() => {
let paragraph = dom.window.document.querySelector('p.wc-md-index').textContent;

expect(paragraph).to.equal(indexPageBody);
});
});

it('should create a default hello page directory', () => {
expect(fs.existsSync(path.join(CONTEXT.publicDir, './hello'))).to.be.true;
});

describe('default generated hello page directory', () => {
const defaultHeading = 'Hello World';
const defaultBody = 'This is an example page built by Greenwood. Make your own in src/pages!';
const helloPageHeading = 'Hello World';
const helloPageBody = 'This is an example page built by Greenwood. Make your own in src/pages!';
let dom;

beforeEach(async() => {
dom = await JSDOM.fromFile(path.resolve(CONTEXT.publicDir, 'hello/index.html'));
dom = await JSDOM.fromFile(path.resolve(CONTEXT.publicDir, './hello', './index.html'));
});

it('should output an index.html file within the default hello page directory', () => {
Expand All @@ -48,13 +74,13 @@ describe('building greenwood with default context (no user workspace)', () => {
it('should have the expected heading text within the hello example page in the hello directory', async() => {
const heading = dom.window.document.querySelector('h3.wc-md-hello').textContent;

expect(heading).to.equal(defaultHeading);
expect(heading).to.equal(helloPageHeading);
});

it('should have the expected paragraph text within the hello example page in the hello directory', async() => {
let paragraph = dom.window.document.querySelector('p.wc-md-hello').textContent;

expect(paragraph).to.equal(defaultBody);
expect(paragraph).to.equal(helloPageBody);
});
});
});
Expand Down

0 comments on commit 4696b4b

Please sign in to comment.