Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rphillips-nz committed Oct 1, 2021
1 parent a494c68 commit 851191e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 25 deletions.
5 changes: 4 additions & 1 deletion src/utility.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const safeStringify = require('fast-safe-stringify');

function normalisePath(path) {
return path.replace(/\/+/, '/').replace(/^\.\//, '');
return path
.replace(/\/+/g, '/')
.replace(/^\.$/, '')
.replace(/^\.\//g, '');
}

function stripTopPath(path, topPath) {
Expand Down
55 changes: 31 additions & 24 deletions test/generator.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const pkginfo = require('pkginfo')(module, 'version');
const info = require('../src/generator.js');
const generator = require('../src/generator.js');

const version = module.exports.version || '';

Expand Down Expand Up @@ -103,15 +103,15 @@ const processedStaticPage = {
// Tests

test('is static page', () => {
expect(info.isStaticPage(staticPage)).toEqual(true);
expect(info.isStaticPage(page)).toBeFalsy();
expect(info.isStaticPage(collectionItem)).toBeFalsy();
expect(generator.isStaticPage(staticPage)).toEqual(true);
expect(generator.isStaticPage(page)).toBeFalsy();
expect(generator.isStaticPage(collectionItem)).toBeFalsy();
});

test('is page', () => {
expect(info.isPage(page)).toEqual(true);
expect(info.isPage(staticPage)).toBeFalsy();
expect(info.isPage(collectionItem)).toBeFalsy();
expect(generator.isPage(page)).toEqual(true);
expect(generator.isPage(staticPage)).toBeFalsy();
expect(generator.isPage(collectionItem)).toBeFalsy();
});

test('gets collections', () => {
Expand All @@ -127,7 +127,7 @@ test('gets collections', () => {
staff: { path: 'staff' }
}

expect(info.getCollections(collectionsConfig, context, config)).toEqual({
expect(generator.getCollections(collectionsConfig, context, config)).toEqual({
pages: [processedPage, processedStaticPage],
staff: [processedCollectionItem]
});
Expand All @@ -146,33 +146,33 @@ test('gets data', () => {
}
};

expect(info.getData(context)).toEqual({
expect(generator.getData(context)).toEqual({
things: ['a', 'b', 'c'],
stuff: {}
});
});

test('get no data', () => {
expect(info.getData({})).toEqual({});
expect(generator.getData({})).toEqual({});
});

test('processes item', () => {
expect(info.processItem(page, 'pages', '.')).toEqual(processedPage);
expect(generator.processItem(page, 'pages', '.')).toEqual(processedPage);
});

test('processes invalid item', () => {
expect(info.processItem({}, 'pages', '.')).toBeUndefined();
expect(generator.processItem({}, 'pages', '.')).toBeUndefined();
});

test('processes unlisted item', () => {
expect(info.processItem(unlistedPage, 'pages', '.')).toEqual({
expect(generator.processItem(unlistedPage, 'pages', '.')).toEqual({
...processedPage,
_unlisted: true
});
});

test('processes non output item', () => {
expect(info.processItem(nonOutputPage, 'pages', '.')).toEqual({
expect(generator.processItem(nonOutputPage, 'pages', '.')).toEqual({
...processedPage,
url: '',
output: false
Expand Down Expand Up @@ -215,18 +215,18 @@ test('gets generator', () => {
pkg: { dependencies: { '@11ty/eleventy': '1' } }
};

expect(info.getGenerator(context, config)).toEqual(processedGenerator);
expect(generator.getGenerator(context, config)).toEqual(processedGenerator);

const contextDev = {
pkg: { devDependencies: { '@11ty/eleventy': '2' } }
};

expect(info.getGenerator(contextDev, config)).toEqual({
expect(generator.getGenerator(contextDev, config)).toEqual({
...processedGenerator,
version: '2'
});

expect(info.getGenerator({}, config)).toEqual({
expect(generator.getGenerator({}, config)).toEqual({
...processedGenerator,
version: ''
});
Expand All @@ -244,8 +244,8 @@ test('gets paths', () => {
cloudcannon: { uploads_dir: 'assets/raw' }
};

expect(info.getPaths({}, config)).toEqual(processedPaths);
expect(info.getPaths(context, config)).toEqual({
expect(generator.getPaths({}, config)).toEqual(processedPaths);
expect(generator.getPaths(context, config)).toEqual({
...processedPaths,
uploads: 'assets/raw'
});
Expand All @@ -259,7 +259,7 @@ test('gets collections config', () => {
}
};

expect(info.getCollectionsConfig(context, config)).toEqual({
expect(generator.getCollectionsConfig(context, config)).toEqual({
pages: {
path: '',
output: true,
Expand All @@ -285,7 +285,7 @@ test('gets complex collections config', () => {
}
};

expect(info.getCollectionsConfig(context, config)).toEqual({
expect(generator.getCollectionsConfig(context, config)).toEqual({
'nested/authors': {
output: true,
path: 'nested/authors'
Expand Down Expand Up @@ -319,7 +319,7 @@ test('gets custom collections config', () => {
}
};

expect(info.getCollectionsConfig(context, config)).toEqual({
expect(generator.getCollectionsConfig(context, config)).toEqual({
anything: 'here'
});
});
Expand Down Expand Up @@ -356,12 +356,15 @@ const processedInfo = {
generator: processedGenerator,
paths: processedPaths,
source: '.',
time: new Date().toISOString(),
time: null,
version: '0.0.2'
};

test('gets info', () => {
const context = {
pkg: {
dependencies: { '@11ty/eleventy': '1' }
},
collections: {
all: [page, collectionItem, staticPage],
staff: [collectionItem]
Expand All @@ -373,5 +376,9 @@ test('gets info', () => {
}
};

expect(info.getInfo(context, config)).toEqual(processedInfo);
const result = generator.getInfo(context, config);
expect({ ...result, time: null }).toEqual(processedInfo);

const time = result.time.substring(0, 10);
expect(time).toEqual(new Date().toISOString().substring(0, 10));
});
29 changes: 29 additions & 0 deletions test/utility.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const utility = require('../src/utility.js');

test('normalises path', () => {
expect(utility.normalisePath('./hello.txt')).toEqual('hello.txt');
expect(utility.normalisePath('.')).toEqual('');
expect(utility.normalisePath('./')).toEqual('');
expect(utility.normalisePath('/abc//def')).toEqual('/abc/def');
});

test('strips top path', () => {
expect(utility.stripTopPath('hello/hello.txt', 'hello/')).toEqual('hello.txt');
expect(utility.stripTopPath('hello/hello.txt', 'hello')).toEqual('/hello.txt');
expect(utility.stripTopPath('hello/hello.txt', '/hello')).toEqual('hello/hello.txt');
expect(utility.stripTopPath('/hello/hello.txt', 'hello')).toEqual('/hello/hello.txt');
expect(utility.stripTopPath('hello/hello.txt', 'goodbye')).toEqual('hello/hello.txt');
});


test('stringifies json', () => {
expect(utility.stringifyJson({ hello: 'there' })).toEqual('{\n\t"hello": "there"\n}')
});

test('stringifies circular json', () => {
const circular = { hello: 'there' };
circular.oops = circular;

const expected = '{\n\t"hello": "there",\n\t"oops": "[Circular]"\n}';
expect(utility.stringifyJson(circular)).toEqual(expected);
});

0 comments on commit 851191e

Please sign in to comment.