Skip to content

Commit

Permalink
Support named component imports #475
Browse files Browse the repository at this point in the history
  • Loading branch information
ovidiuch committed Nov 14, 2017
1 parent d698995 commit 882f0e5
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 3 deletions.
@@ -0,0 +1,7 @@
// @flow

import React from 'react';

export const CONST = 'foo';

export const Italic = ({ name }: { name: string }) => <em>{name}</em>;
@@ -0,0 +1,8 @@
// @flow

import { Italic } from './Italic';

export default {
component: Italic,
props: {}
};
@@ -0,0 +1,8 @@
// @flow

import { CONST, Italic } from './Italic';

export default {
component: Italic,
props: {}
};
@@ -0,0 +1,50 @@
// @flow

import { join } from 'path';
import { findFixtureFiles } from '../../../find-fixture-files';

const { resolve } = require;

describe('ES module / Single fixture with named component import', () => {
let files;

beforeEach(async () => {
files = await findFixtureFiles({
rootPath: join(__dirname, '__fsmocks__')
});
});

describe('single named import', () => {
it('has fixture path', () => {
expect(files[0].filePath).toBe(resolve('./__fsmocks__/fixture'));
});

it('has component name', () => {
expect(files[0].components[0].name).toBe('Italic');
});

it('has component path', () => {
expect(files[0].components[0].filePath).toBe(
resolve('./__fsmocks__/Italic')
);
});
});

describe('multi named imports', () => {
it('has fixture path', () => {
expect(files[1].filePath).toBe(
resolve('./__fsmocks__/multi-imports.fixture')
);
});

it('has component name', () => {
expect(files[1].components[0].name).toBe('Italic');
});

it('has component path', () => {
expect(files[1].components[0].filePath).toBe(
resolve('./__fsmocks__/Italic')
);
});
});
});
Expand Up @@ -131,9 +131,8 @@ export async function extractComponentsFromFixtureFile(
}

function getImportPathByName(imports, importName: string): string | null {
// TODO: Support `import { component }` or `import { component as component }`
const relevantImport = imports.find(
i => i.specifiers[0].local.name === importName
const relevantImport = imports.find(i =>
i.specifiers.some(s => s.local.name === importName)
);

return relevantImport ? relevantImport.source.value : null;
Expand Down

0 comments on commit 882f0e5

Please sign in to comment.