-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fluent-api): add fluent api (builder) functionallity and persist…
…ance Add fluent API to enable methods chanining with ability to persist the mock BREAKING CHANGE: MockFactory is now a function and not a class, changed the original to MockGenerator. Add fluent API and ability to persist mock datta closes #42, closes #37
- Loading branch information
Showing
6 changed files
with
151 additions
and
10 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
sample/mockingbird-typeorm/test/__mocks__/app-e2e.test.mock.json
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,14 @@ | ||
{ | ||
"Dog": { | ||
"this-is-a-slug": [ | ||
{ | ||
"name": "Rexy", | ||
"point": 10, | ||
}, | ||
{ | ||
"name": "Rexy", | ||
"point": 10 | ||
} | ||
] | ||
} | ||
} |
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,63 @@ | ||
import { MockGenerator } from '../factories'; | ||
import { Class, ClassLiteral, GeneratedMock } from '../types'; | ||
import { MockPersistent } from './mock-persistent'; | ||
|
||
export class MockBuilder<TClass extends Class> { | ||
private doPersist = false; | ||
private slug = undefined; | ||
private isPlain = false; | ||
private locale = 'en'; | ||
|
||
public constructor(private readonly targetClass: Class, private readonly persistent: MockPersistent) {} | ||
|
||
private process(mock: TClass[]): TClass[] | ClassLiteral<TClass>[]; | ||
private process(mock: TClass): TClass | ClassLiteral<TClass>; | ||
|
||
private process(mock: TClass | TClass[]): GeneratedMock | TClass[] | ClassLiteral<TClass>[] { | ||
let newMock; | ||
|
||
if (this.isPlain) { | ||
if (mock.length > 1) { | ||
newMock = (mock as TClass[]).map((mock) => Object.assign({}, mock)); | ||
} | ||
|
||
newMock = Object.assign({}, mock); | ||
} | ||
|
||
if (this.doPersist) { | ||
return this.persistent.process({ | ||
slug: this.slug, | ||
baseMock: newMock, | ||
}); | ||
} | ||
|
||
return newMock; | ||
} | ||
|
||
public setLocale(locale: string): this { | ||
this.locale = locale; | ||
return this; | ||
} | ||
|
||
public toPlain(): this { | ||
this.isPlain = true; | ||
return this; | ||
} | ||
|
||
public many(count: number): TClass[] { | ||
const mock: TClass[] = MockGenerator.create<TClass>(this.targetClass, { locale: this.locale, count }); | ||
return this.process(mock); | ||
} | ||
|
||
public one(): TClass { | ||
const mock: TClass = MockGenerator.create<TClass>(this.targetClass); // TODO: Add locale | ||
return this.process(mock); | ||
} | ||
|
||
public persist(slug: string): this { | ||
this.doPersist = true; | ||
this.slug = slug; | ||
|
||
return this; | ||
} | ||
} |
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,16 @@ | ||
import fs from 'fs'; | ||
import { Class } from '../types'; | ||
import { MockBuilder } from './mock-builder'; | ||
import { MockPersistent } from './mock-persistent'; | ||
|
||
export function MockFactory<TClass extends Class = any>(target: Class): MockBuilder<TClass> { | ||
const persistent = new MockPersistent(fs, { | ||
fileName: '', | ||
}); | ||
|
||
return new MockBuilder<TClass>(target, persistent); | ||
} | ||
|
||
class Dog {} | ||
|
||
MockFactory(Dog).toPlain().persist('lots-of-dogs').many(3); |
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,48 @@ | ||
import * as fileSystem from 'fs'; | ||
import { GeneratedMock } from '../types'; | ||
|
||
export interface PersistentDto { | ||
baseMock: GeneratedMock; | ||
slug: string; | ||
} | ||
|
||
export class MockPersistent { | ||
private mockData: GeneratedMock = {}; | ||
|
||
public constructor(private readonly fs: typeof fileSystem, private readonly options: { fileName: string }) { | ||
this.readFile(); | ||
} | ||
|
||
private readFile() { | ||
const { fileName } = this.options; | ||
|
||
if (this.fs.existsSync(fileName)) { | ||
try { | ||
const fileData = this.fs.readFileSync(fileName, 'utf-8'); | ||
this.mockData = JSON.parse(fileData); | ||
} catch (e) { | ||
console.error(`The mock file '${fileName}' can not be parsed due to JSON error`); | ||
} | ||
} | ||
} | ||
|
||
private saveFile() { | ||
const data = JSON.stringify(this.mockData); | ||
this.fs.writeFileSync(this.options.fileName, data); | ||
} | ||
|
||
public process(config: PersistentDto): GeneratedMock { | ||
const { baseMock, slug } = config; | ||
|
||
if (this.mockData.hasOwnProperty(slug)) { | ||
return this.mockData[slug]; | ||
} | ||
|
||
console.log(`The slug '${slug}' can not be found inside '${this.options.fileName}', creating one..`); | ||
|
||
this.mockData[slug] = baseMock; | ||
this.saveFile(); | ||
|
||
return baseMock; | ||
} | ||
} |