Skip to content

Commit

Permalink
Factory test function make, makeAs, create, createAs, raw, rawOf
Browse files Browse the repository at this point in the history
  • Loading branch information
frjjkil2009 committed May 16, 2018
1 parent b8b8e42 commit 73c8363
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 50 deletions.
171 changes: 171 additions & 0 deletions integration/test/features/Factory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import 'jest'
import { Eloquent, Factory, Faker, factory } from '../../../dist/lib'
import { User, init_mongoose } from '../../mongodb/index'
import * as Validator from 'validator'

interface ITest {
name: string
status: string
is_test: boolean
created_at: Date
updated_at: Date
deleted_at: Date | null
}

interface Test extends ITest { }
class Test extends Eloquent<ITest> {
static className: string = 'Test'
static timestamps = true
static softDeletes = true
static fillable = ['name', 'status', 'is_test']
static schema = {
name: { type: String, required: true },
status: { type: String },
is_test: { type: Boolean }
}

getClassName() {
return Test.className
}
}

Factory.define(Test, (faker: Faker, attributes?: Object): Object => {
return Object.assign(
{
name: faker.name()
},
attributes
)
})
Factory.defineAs(Test, 'test', (faker: Faker, attributes?: Object): Object => {
return Object.assign(
{
name: faker.name(),
is_test: true
},
attributes
)
})
Factory.state(Test, 'tested', () => {
return {
status: 'tested'
}
})
Factory.state(Test, 'testing', () => {
return {
status: 'testing'
}
})

function expectUser(user: User) {
expect(Validator.isMongoId(user.id)).toBe(true)
expect(typeof (user.first_name)).toEqual('string')
expect(typeof (user.last_name)).toEqual('string')
expect(Validator.isEmail(user.email)).toBe(true)
expect(user.deleted_at).toBeNull()
expect(typeof (user.age)).toEqual('number')
}

function expectTest(test: Test) {
expect(Validator.isMongoId(test.id)).toBe(true)
expect(typeof (test.name)).toEqual('string')
expect(test.deleted_at).toBeNull()
}

describe('Factory', function () {
beforeAll(async function () {
await init_mongoose('integration_factory')
})

// afterEach(async function () {
// await delete_collection(['users'])
// })

describe('.make()', function () {
it('can make an instance of User', function () {
const testUser = Factory.make(User)
expectUser(testUser)
})

it('can make an instance of Test with status is tested', function () {
const test = factory(Test).states(['tested']).make()
expectTest(test)
expect(test.status).toEqual('tested')
})

it('can make an instance of Test with status is testing', function () {
const test = factory(Test).states(['testing']).make()
expectTest(test)
expect(test.status).toEqual('testing')
})
})

describe('.makeAs()', function () {
it('can make as an instance of Test', function () {
const test = Factory.makeAs(Test, 'test')
expectTest(test)
expect(test.is_test).toBe(true)
})
})

describe('.create()', function () {
it('can create an instance of User', async function () {
const testUser = await Factory.create(User)
expectUser(testUser)
expect(testUser.created_at instanceof Date).toBe(true)
expect(testUser.updated_at instanceof Date).toBe(true)
})

it('can create an instance of Test with status is tested', async function () {
const test = await factory(Test).states(['tested']).create()
expectTest(test)
expect(test.status).toEqual('tested')
expect(test.created_at instanceof Date).toBe(true)
expect(test.updated_at instanceof Date).toBe(true)
})

it('can create an instance of Test with status is testing', async function () {
const test = await factory(Test).states(['testing']).create()
expectTest(test)
expect(test.status).toEqual('testing')
expect(test.created_at instanceof Date).toBe(true)
expect(test.updated_at instanceof Date).toBe(true)
})

it('can create 5 instance of User', async function () {
const users = await factory(User).times(5).create()
expect(users.count()).toEqual(5)
})
})

describe('.createAs()', function () {
it('can create as an instance of Test', async function () {
const test = await Factory.createAs(Test, 'test')
expectTest(test)
expect(test.created_at instanceof Date).toBe(true)
expect(test.updated_at instanceof Date).toBe(true)
expect(test.is_test).toBe(true)
})
})

describe('.rawOf()', function () {
it('can raw of an instance of Test', function () {
const test = Factory.rawOf(Test, 'test')
expect(test).toMatchObject({ is_test: true })
})
})

describe('.raw()', function () {
it('can raw an instance of Test', function () {
const test = Factory.raw(Test, { name: 'test' })
expect(test).toMatchObject({ name: 'test' })
})
})

describe('.getFacade()', function () {
it('can raw an instance of Test', function () {
const test = Factory.getFacade().createMock()
console.log(test)
})
})
})
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 15 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,23 @@
"skipBabel": true
}
},
"moduleFileExtensions": ["js", "ts", "json"],
"moduleFileExtensions": [
"js",
"ts",
"json"
],
"transform": {
"^.+\\.(ts|tsx)$": "./node_modules/ts-jest/preprocessor.js"
},
"testMatch": ["**/test/**/*.test.ts", "**/test/**/*.test.js"],
"testMatch": [
"**/test/**/*.test.ts",
"**/test/**/*.test.js"
],
"testEnvironment": "node"
},
"files": ["dist/**/*"],
"files": [
"dist/**/*"
],
"main": "./dist/lib/index.js",
"types": "./dist/lib/index.d.ts",
"repository": {
Expand Down Expand Up @@ -65,6 +74,7 @@
"@types/mongoose": "^4.7.31",
"@types/pluralize": "^0.0.28",
"@types/sinon": "^4.1.2",
"@types/validator": "^9.4.1",
"collect.js": "^4.0.11",
"coveralls": "^3.0.0",
"jest": "^21.2.1",
Expand All @@ -73,6 +83,7 @@
"sinon": "^4.1.3",
"ts-jest": "^21.2.3",
"tslint": "^5.8.0",
"typescript": "^2.6.2"
"typescript": "^2.6.2",
"validator": "^10.2.0"
}
}
56 changes: 10 additions & 46 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
version "4.1.3"
resolved "https://registry.yarnpkg.com/@types/sinon/-/sinon-4.1.3.tgz#2ee25e0e302f31e78a945650a60029e08878eaf8"

"@types/validator@^9.4.1":
version "9.4.1"
resolved "https://registry.yarnpkg.com/@types/validator/-/validator-9.4.1.tgz#bea5a290e61f1cbf12af3fd878706aeec2ba0087"

abab@^1.0.3:
version "1.0.4"
resolved "https://registry.yarnpkg.com/abab/-/abab-1.0.4.tgz#5faad9c2c07f60dd76770f71cf025b62a63cfd4e"
Expand Down Expand Up @@ -132,18 +136,12 @@ ansi-styles@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-2.2.1.tgz#b432dd3358b634cf75e1e4664368240533c1ddbe"

ansi-styles@^3.1.0:
ansi-styles@^3.1.0, ansi-styles@^3.2.0:
version "3.2.1"
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.2.1.tgz#41fbb20243e50b12be0f04b8dedbf07520ce841d"
dependencies:
color-convert "^1.9.0"

ansi-styles@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.2.0.tgz#c159b8d5be0f9e5a6f346dab94f16ce022161b88"
dependencies:
color-convert "^1.9.0"

anymatch@^1.3.0:
version "1.3.2"
resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-1.3.2.tgz#553dcb8f91e3c889845dfdba34c77721b90b9d7a"
Expand Down Expand Up @@ -593,7 +591,7 @@ center-align@^0.1.1:
align-text "^0.1.3"
lazy-cache "^1.0.3"

chalk@2.3.0:
chalk@2.3.0, chalk@^2.0.1, chalk@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.3.0.tgz#b5ea48efc9c1793dccc9b4767c93914d3f2d52ba"
dependencies:
Expand All @@ -611,14 +609,6 @@ chalk@^1.1.3:
strip-ansi "^3.0.0"
supports-color "^2.0.0"

chalk@^2.0.1, chalk@^2.3.0:
version "2.3.1"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.3.1.tgz#523fe2678aec7b04e8041909292fe8b17059b796"
dependencies:
ansi-styles "^3.2.0"
escape-string-regexp "^1.0.5"
supports-color "^5.2.0"

chance@^1.0.13:
version "1.0.13"
resolved "https://registry.yarnpkg.com/chance/-/chance-1.0.13.tgz#666bec2db42b3084456a3e4f4c28a82db5ccb7e6"
Expand Down Expand Up @@ -710,11 +700,7 @@ combined-stream@1.0.6, combined-stream@^1.0.5, combined-stream@~1.0.5:
dependencies:
delayed-stream "~1.0.0"

commander@^2.12.1:
version "2.14.1"
resolved "https://registry.yarnpkg.com/commander/-/commander-2.14.1.tgz#2235123e37af8ca3c65df45b026dbd357b01b9aa"

commander@^2.13.0:
commander@^2.12.1, commander@^2.13.0:
version "2.15.1"
resolved "https://registry.yarnpkg.com/commander/-/commander-2.15.1.tgz#df46e867d0fc2aec66a34662b406a9ccafff5b0f"

Expand Down Expand Up @@ -2795,10 +2781,6 @@ process-nextick-args@~1.0.6:
version "1.0.7"
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-1.0.7.tgz#150e20b756590ad3f91093f25a4f2ad8bff30ba3"

process-nextick-args@~2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.0.tgz#a37d732f4271b4ab1ad070d35508e8290788ffaa"

prr@~1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/prr/-/prr-1.0.1.tgz#d3fc114ba06995a45ec6893f484ceb1d78f5f476"
Expand Down Expand Up @@ -2865,7 +2847,7 @@ read-pkg@^2.0.0:
normalize-package-data "^2.3.2"
path-type "^2.0.0"

readable-stream@2.3.3:
readable-stream@2.3.3, readable-stream@^2.0.2, readable-stream@^2.0.6, readable-stream@^2.1.4:
version "2.3.3"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.3.tgz#368f2512d79f9d46fdfc71349ae7878bbc1eb95c"
dependencies:
Expand All @@ -2877,18 +2859,6 @@ readable-stream@2.3.3:
string_decoder "~1.0.3"
util-deprecate "~1.0.1"

readable-stream@^2.0.2, readable-stream@^2.0.6, readable-stream@^2.1.4:
version "2.3.4"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.4.tgz#c946c3f47fa7d8eabc0b6150f4a12f69a4574071"
dependencies:
core-util-is "~1.0.0"
inherits "~2.0.3"
isarray "~1.0.0"
process-nextick-args "~2.0.0"
safe-buffer "~5.1.1"
string_decoder "~1.0.3"
util-deprecate "~1.0.1"

readdirp@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/readdirp/-/readdirp-2.1.0.tgz#4ed0ad060df3073300c48440373f72d1cc642d78"
Expand Down Expand Up @@ -3031,13 +3001,7 @@ resolve@1.1.7:
version "1.1.7"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.1.7.tgz#203114d82ad2c5ed9e8e0411b3932875e889e97b"

resolve@^1.1.6:
version "1.6.0"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.6.0.tgz#0fbd21278b27b4004481c395349e7aba60a9ff5c"
dependencies:
path-parse "^1.0.5"

resolve@^1.1.7, resolve@^1.3.2:
resolve@^1.1.6, resolve@^1.1.7, resolve@^1.3.2:
version "1.5.0"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.5.0.tgz#1f09acce796c9a762579f31b2c1cc4c3cddf9f36"
dependencies:
Expand Down Expand Up @@ -3384,7 +3348,7 @@ supports-color@^4.0.0:
dependencies:
has-flag "^2.0.0"

supports-color@^5.1.0, supports-color@^5.2.0:
supports-color@^5.1.0:
version "5.2.0"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.2.0.tgz#b0d5333b1184dd3666cbe5aa0b45c5ac7ac17a4a"
dependencies:
Expand Down

0 comments on commit 73c8363

Please sign in to comment.