Merge pull request #26 from kawwong/get-component-type

Create getComponentType functions and helper functions
This commit is contained in:
manpatel3107 2019-03-06 12:23:28 -08:00 коммит произвёл GitHub
Родитель f4d5dcab7e feb4c84e96
Коммит fcdde989b5
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
9 изменённых файлов: 158 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,9 @@
import isNgComponent from './isNgComponent';
import isNgModule from './isNgModule';
import isReact from './isReact';
export default {
isNgComponent,
isNgModule,
isReact
};

Просмотреть файл

@ -0,0 +1,15 @@
import isNgComponent from './isNgComponent';
describe('isAngular.ts', () => {
it('should return false when fileContents is null', () => {
expect(isNgComponent(null)).toBeFalsy();
});
it('should return false when fileContents does not contain @Component or @Module', () => {
expect(isNgComponent('test')).toBeFalsy();
});
it('should return true when fileContents contains @Component', () => {
expect(isNgComponent('@Component')).toBeTruthy();
});
});

Просмотреть файл

@ -0,0 +1,11 @@
function isNgComponent (fileContents: string): boolean {
let isNgComponent = false;
if (fileContents != null) {
isNgComponent = fileContents.indexOf('@Component') >= 0;
}
return isNgComponent;
}
export default isNgComponent;

Просмотреть файл

@ -0,0 +1,15 @@
import isNgModule from './isNgModule';
describe('isAngular.ts', () => {
it('should return false when fileContents is null', () => {
expect(isNgModule(null)).toBeFalsy();
});
it('should return false when fileContents does not contain @Component or @Module', () => {
expect(isNgModule('test')).toBeFalsy();
});
it('should return true when fileContents contains @Module', () => {
expect(isNgModule('@NgModule')).toBeTruthy();
});
});

Просмотреть файл

@ -0,0 +1,11 @@
function isNgModule (fileContents: string): boolean {
let isNgModule = false;
if (fileContents != null) {
isNgModule = fileContents.indexOf('@NgModule') >= 0;
}
return isNgModule;
}
export default isNgModule;

Просмотреть файл

@ -0,0 +1,23 @@
import isReact from './isReact';
describe('isReact.ts', () => {
it('should return false when fileContents is null', () => {
expect(isReact(null)).toBeFalsy();
});
it('should return false when fileContents does not contain the React import from react', () => {
expect(isReact(`import { Component } from 'react'`)).toBeFalsy();
});
it('should return true when fileContents contains the React import from react', () => {
expect(isReact(`import React from 'react'`)).toBeTruthy();
});
it('should return true when fileContents contains the React import last from react', () => {
expect(isReact(`import { Component }, React from 'react'`)).toBeTruthy();
});
it('should return true when fileContents contains the React not last from react', () => {
expect(isReact(`import React, { Component } from 'react'`)).toBeTruthy();
});
});

Просмотреть файл

@ -0,0 +1,17 @@
function isReact (fileContents: string): boolean {
let hasReactImport = false;
if (fileContents != null) {
const importIndex = fileContents.indexOf(`from 'react'`);
if (importIndex > 0) {
const importPortion = fileContents.substring(0, importIndex);
hasReactImport = importPortion.indexOf(' React ') > 0
|| importPortion.indexOf(' React,') > 0;
}
}
return hasReactImport;
}
export default isReact;

Просмотреть файл

@ -0,0 +1,40 @@
import getComponentType from '.';
import helpers from './helpers';
describe('index.ts', () => {
afterEach(() => {
jest.clearAllMocks();
});
it('should return null when the input does not return true for any framework', () => {
helpers.isNgComponent = jest.fn(() => false);
helpers.isNgModule = jest.fn(() => false);
helpers.isReact = jest.fn(() => false);
const framework = getComponentType('test');
expect(framework).toBeNull();
});
it('should return ngComponent when isNgComponent is true', () => {
helpers.isNgComponent = jest.fn(() => true);
helpers.isNgModule = jest.fn(() => false);
helpers.isReact = jest.fn(() => false);
const framework = getComponentType('test');
expect(framework).toBe('ngComponent');
});
it('should return ngModule when isNgModule is true', () => {
helpers.isNgComponent = jest.fn(() => false);
helpers.isNgModule = jest.fn(() => true);
helpers.isReact = jest.fn(() => false);
const framework = getComponentType('test');
expect(framework).toBe('ngModule');
});
it('should return react when isReact is true', () => {
helpers.isNgComponent = jest.fn(() => false);
helpers.isNgModule = jest.fn(() => false);
helpers.isReact = jest.fn(() => true);
const framework = getComponentType('test');
expect(framework).toBe('react');
});
});

Просмотреть файл

@ -0,0 +1,17 @@
import helpers from './helpers';
function getComponentType (fileContents: string): string {
let componentType = null;
if (helpers.isNgComponent(fileContents)) {
componentType = 'ngComponent';
} else if (helpers.isNgModule(fileContents)) {
componentType = 'ngModule';
} else if (helpers.isReact(fileContents)) {
componentType = 'react';
}
return componentType;
}
export default getComponentType;