Skip to content

Commit

Permalink
test: add tests for internal util evaluateAbellBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabhdaware committed Mar 2, 2022
1 parent 4d55982 commit 42aa849
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 48 deletions.
93 changes: 46 additions & 47 deletions packages/abell/src/utils/__tests__/api.spec.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,48 @@
// import { test, describe, expect } from 'vitest';
// import { makeRoutesFromGlobImport } from '../api';
// import { prefix } from './test-utils';
import { test, describe, expect } from 'vitest';
import { makeRoutesFromGlobImport } from '../api';

// describe('makeRoutesFromGlobImport()', () => {
// test('should return relative abell files', () => {
// const abellPages = {
// './src/index.abell': {
// default: () => 'hehe'
// },
// './src/about.abell': {
// default: () => 'hehe'
// },
// './src/nested/index.abell': {
// default: () => 'hehe'
// }
// };
// expect(findAbellFileFromURL('/', abellPages)).toBe('./src/index.abell');
// expect(findAbellFileFromURL('/about', abellPages)).toBe(
// './src/about.abell'
// );
// expect(findAbellFileFromURL('/nested', abellPages)).toBe(
// './src/nested/index.abell'
// );
// });
describe('makeRoutesFromGlobImport()', () => {
const abellPages = {
'./src/index.abell': {
default: () => 'index'
},
'./src/about.abell': {
default: () => 'about'
},
'./src/nested/index.abell': {
default: () => 'nested'
},
'./src/_components/navbar.abell': {
default: () => 'nav'
},
'./src/components/_navbar.abell': {
default: () => 'nav2'
}
};

// test('should return absolute abell files', () => {
// const abellPages = {
// [prefix('./src/index.abell')]: {
// default: () => 'hehe'
// },
// [prefix('./src/about.abell')]: {
// default: () => 'hehe'
// },
// [prefix('./src/nested/index.abell')]: {
// default: () => 'hehe'
// }
// };
// expect(findAbellFileFromURL('/', abellPages)).toBe(
// prefix('./src/index.abell')
// );
// expect(findAbellFileFromURL('/about', abellPages)).toBe(
// prefix('./src/about.abell')
// );
// expect(findAbellFileFromURL('/nested', abellPages)).toBe(
// prefix('./src/nested/index.abell')
// );
// });
// });
test('should return routes object based on abellPages', () => {
const routes = makeRoutesFromGlobImport(abellPages);
const paths = routes.map((route) => route.path);
const renderOutputs = routes.map((route) => route.render());
expect(routes[0]).toHaveProperty('path');
expect(routes[0]).toHaveProperty('render');
expect(paths).toEqual(['/', '/about', '/nested']);
expect(renderOutputs).toEqual(['index', 'about', 'nested']);
});

test('should allow underscore routes with ignoreUnderScoreURLs false', () => {
const routes = makeRoutesFromGlobImport(abellPages, {
ignoreUnderscoreURLs: false
});
const paths = routes.map((route) => route.path);
const renderOutputs = routes.map((route) => route.render());
expect(paths).toEqual([
'/',
'/about',
'/nested',
'/_components/navbar',
'/components/_navbar'
]);
expect(renderOutputs).toEqual(['index', 'about', 'nested', 'nav', 'nav2']);
});
});
37 changes: 36 additions & 1 deletion packages/abell/src/utils/__tests__/internal-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test, describe, expect } from 'vitest';
import { getURLFromFilePath } from '../internal-utils';
import { getURLFromFilePath, evaluateAbellBlock } from '../internal-utils';
import { BASE_PATH, prefix } from './test-utils';

describe('getURLFromFilePath()', () => {
Expand All @@ -24,3 +24,38 @@ describe('getURLFromFilePath()', () => {
);
});
});

describe('evaluateAbellBlock()', () => {
test('should return evaluated number', () => {
expect(evaluateAbellBlock(3 + 2)).toBe(5);
});

test('should return the return of function', () => {
expect(evaluateAbellBlock(() => 3)).toBe(3);
});

test('should return an empty string on undefined and null', () => {
expect(evaluateAbellBlock(undefined)).toBe('');
expect(evaluateAbellBlock(null)).toBe('');
});

test('should return exact string on giving string', () => {
expect(evaluateAbellBlock('hello')).toBe('hello');
});

test('should return false on false', () => {
expect(evaluateAbellBlock(false)).toBe(false);
});

test('should return true on true', () => {
expect(evaluateAbellBlock(true)).toBe(true);
});

test('should join an array', () => {
expect(evaluateAbellBlock([1, 2, 3])).toBe('123');
});

test('should return 0 on given 0', () => {
expect(evaluateAbellBlock(0)).toBe(0);
});
});
5 changes: 5 additions & 0 deletions packages/abell/src/utils/internal-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export const findIndexPath = (abellPages: AbellPagesGlobImport): string => {
abellPage.endsWith('index.abell')
);

// Only one index file in import
if (abellIndexPaths.length === 1) {
return abellIndexPaths[0];
}

// index.abell with shortest path is going to root index.abell
let shortestPathLength = abellIndexPaths[0].length;
let likelyRootIndexPath = abellIndexPaths[0];
Expand Down

0 comments on commit 42aa849

Please sign in to comment.