Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #774: Adding support to location array #843

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions loaders/__tests__/styleguide-loader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,34 @@ it('should return correct component paths: function returning absolute paths', (
expect(result).not.toMatch(`'filepath': 'components/RandomButton/RandomButton.js'`);
});

it('should return correct component paths: function returning absolute paths', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function returning absolute paths → array of component absolute paths.

You can also add a test for relative paths.

const result = styleguideLoader.pitch.call(
{
request: file,
_styleguidist: {
sections: [
{
components: [
`${__dirname}/components/Button/Button.js`,
`${__dirname}/components/Placeholder/Placeholder.js`,
],
},
],
configDir: __dirname,
getExampleFilename: () => 'Readme.md',
getComponentPathLine: filepath => filepath,
},
addContextDependency: () => {},
},
readFileSync(file, 'utf8')
);
expect(result).toBeTruthy();
expect(() => new vm.Script(result)).not.toThrow();
expect(result).toMatch(`'filepath': 'components/Button/Button.js'`);
expect(result).toMatch(`'filepath': 'components/Placeholder/Placeholder.js'`);
expect(result).not.toMatch(`'filepath': 'components/RandomButton/RandomButton.js'`);
});

it('should return correct component paths: function returning relative paths', () => {
const result = styleguideLoader.pitch.call(
{
Expand Down
3 changes: 3 additions & 0 deletions loaders/utils/getComponentFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const path = require('path');
const glob = require('glob');
const isFunction = require('lodash/isFunction');
const isArray = require('lodash/isArray');
const isString = require('lodash/isString');

/**
Expand All @@ -21,6 +22,8 @@ module.exports = function getComponentFiles(components, rootDir, ignore) {
let componentFiles;
if (isFunction(components)) {
componentFiles = components();
} else if (isArray(components)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Array.isArray.

componentFiles = components;
} else if (isString(components)) {
componentFiles = glob.sync(path.resolve(rootDir, components), { ignore });
} else {
Expand Down
27 changes: 26 additions & 1 deletion scripts/__tests__/config.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isFunction, isArray } from 'lodash';
import fs from 'fs';
import path from 'path';
import getConfig from '../config';
Expand Down Expand Up @@ -35,12 +36,36 @@ it('should accept config as an object', () => {
expect(result).toMatchObject({ title: 'Style guide' });
});

it('should accept components as a string', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests don’t add much (if any) value: you’re testing this line:

type: ['string', 'function', 'array'],

which is already tested in config sanitizer tests.

const result = getConfig({
components: './components/**/[A-Z]*.js',
});
expect(result).toHaveProperty('components');
expect(result.components).toBe('./components/**/[A-Z]*.js');
});

it('should accept components as a function', () => {
const result = getConfig({
components: () => ['./components/**/[A-Z]*.js'],
});
expect(result).toHaveProperty('components');
expect(isFunction(result.components)).toBeTruthy();
});

it('should accept components as an array', () => {
const result = getConfig({
components: ['./components/**/[A-Z]*.js'],
});
expect(result).toHaveProperty('components');
expect(isArray(result.components)).toBeTruthy();
});

it('should throw if config has errors', () => {
const fn = () =>
getConfig({
components: 42,
});
expect(fn).toThrowError('should be string or function');
expect(fn).toThrowError('should be string, function, or array');
});

it('should change the config using the update callback', () => {
Expand Down
2 changes: 1 addition & 1 deletion scripts/schemas/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = {
},
// `components` is a shortcut for { sections: [{ components }] }, see `sections` below
components: {
type: ['string', 'function'],
type: ['string', 'function', 'array'],
example: 'components/**/[A-Z]*.js',
},
configDir: {
Expand Down