-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
hirsch
committed
Jun 7, 2019
1 parent
9f0522e
commit c3a3c39
Showing
10 changed files
with
130 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { EntityFactory } from './entity-factory' | ||
|
||
describe('make', () => { | ||
// tslint:disable-next-line | ||
class User { | ||
constructor(public name: string) {} | ||
} | ||
// tslint:disable-next-line | ||
class Pet { | ||
constructor(public name: string, public user: User) {} | ||
} | ||
|
||
test('Should make a new entity', async () => { | ||
const mockUserFactory = jest.fn() | ||
const userFactory = new EntityFactory('User', User, mockUserFactory) | ||
mockUserFactory.mockReturnValue(new User('Steve')) | ||
|
||
const newUser = await userFactory.make() | ||
|
||
expect(newUser.name).toBe('Steve') | ||
}) | ||
|
||
test('Should override the enitys props', async () => { | ||
const mockUserFactory = jest.fn() | ||
const userFactory = new EntityFactory('User', User, mockUserFactory) | ||
mockUserFactory.mockReturnValue(new User('Steve')) | ||
|
||
const newUser = await userFactory.make({ name: 'Tony' }) | ||
|
||
expect(newUser.name).toBe('Tony') | ||
}) | ||
|
||
test('Should call the nested entity factories', async () => { | ||
const mockUserFactory = jest.fn() | ||
const userFactory = new EntityFactory('User', User, mockUserFactory) | ||
|
||
const mockPetFactory = jest.fn() | ||
const petFactory = new EntityFactory('Pet', Pet, mockPetFactory) | ||
|
||
mockUserFactory.mockReturnValue({ | ||
name: 'Pepper', | ||
}) | ||
|
||
mockPetFactory.mockReturnValue({ | ||
name: 'Bunny', | ||
user: userFactory, | ||
}) | ||
|
||
const newPet = await petFactory.make() | ||
|
||
expect(newPet.name).toBe('Bunny') | ||
expect(newPet.user.name).toBe('Pepper') | ||
}) | ||
|
||
test('Should call the map function', async () => { | ||
const mockUserFactory = jest.fn() | ||
const userFactory = new EntityFactory('User', User, mockUserFactory) | ||
mockUserFactory.mockReturnValue(new User('Steve')) | ||
const mockMap = jest.fn() | ||
|
||
const newUsers = await userFactory.map(mockMap).makeMany(2) | ||
|
||
expect(newUsers.length).toBe(2) | ||
expect(mockMap).toBeCalledTimes(2) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { times } from './helpers' | ||
|
||
describe('times', () => { | ||
test('Should call the func 2 times ', async () => { | ||
const result = await times<string>(2, async () => 'a') | ||
|
||
expect(result.length).toBe(2) | ||
expect(result).toStrictEqual(['a', 'a']) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { loadFiles } from './file.util' | ||
import * as path from 'path' | ||
import * as glob from 'glob' | ||
|
||
describe('loadFiles', () => { | ||
let syncMock: jest.Mock | ||
beforeEach(() => { | ||
syncMock = jest.fn() | ||
;(glob as any).sync = syncMock | ||
syncMock.mockReturnValue(['fileA', 'fileB']) | ||
}) | ||
|
||
test('Should return a flat array', () => { | ||
const results = loadFiles(['path/to/files', 'other/path/to/files']) | ||
|
||
expect(results.length).toBe(4) | ||
expect(results[0]).toBe('fileA') | ||
expect(results[1]).toBe('fileB') | ||
}) | ||
|
||
test('Should call the sync method with the cwd path', () => { | ||
const results = loadFiles(['path/to/files']) | ||
|
||
expect(syncMock).toBeCalledWith(path.join(process.cwd(), 'path/to/files')) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
import glob from 'glob' | ||
import * as glob from 'glob' | ||
import * as path from 'path' | ||
|
||
export const importFiles = (filePaths: string[]) => filePaths.forEach(require) | ||
|
||
export const loadFiles = (filePattern: string[]): string[] => { | ||
const filePaths: string[] = filePattern | ||
return filePattern | ||
.map(pattern => glob.sync(path.join(process.cwd(), pattern))) | ||
.reduce((acc, filePath) => acc.concat(filePath), []) | ||
return filePaths | ||
} |