-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(*): make knex.config required
Also add more tests. BREAKING CHANGE: config is now required Signed-off-by: Will Soto <[email protected]>
- Loading branch information
Showing
7 changed files
with
212 additions
and
17 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 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
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,198 @@ | ||
import { | ||
KNEX_CONNECTION, | ||
OBJECTION_BASE_MODEL, | ||
OBJECTION_MODULE_OPTIONS | ||
} from "@/constants"; | ||
import { ObjectionCoreModule } from "@/core"; | ||
import { | ||
ObjectionModuleOptions, | ||
ObjectionModuleOptionsFactory | ||
} from "@/interfaces"; | ||
import { Injectable } from "@nestjs/common"; | ||
import { Test, TestingModule } from "@nestjs/testing"; | ||
import knex from "knex"; | ||
import { Model } from "objection"; | ||
|
||
describe("ObjectionCoreModule", () => { | ||
let testingModule: TestingModule; | ||
const config: knex.Config = { | ||
client: "sqlite3", | ||
useNullAsDefault: true, | ||
connection: { | ||
filename: "./testing.sqlite" | ||
} | ||
}; | ||
|
||
describe("#forRoot", () => { | ||
beforeEach(async () => { | ||
testingModule = await Test.createTestingModule({ | ||
imports: [ | ||
ObjectionCoreModule.forRoot({ | ||
config | ||
}) | ||
] | ||
}).compile(); | ||
}); | ||
|
||
test("provides a connection", () => { | ||
const connection = testingModule.get<knex>("KnexConnection"); | ||
|
||
expect(connection).toBeDefined(); | ||
}); | ||
|
||
test("provides a base model", () => { | ||
const model = testingModule.get<Model>("ObjectionBaseModel"); | ||
|
||
expect(model).toBeDefined(); | ||
}); | ||
}); | ||
|
||
describe("#forRootAsync", () => { | ||
beforeEach(async () => { | ||
testingModule = await Test.createTestingModule({ | ||
imports: [ | ||
ObjectionCoreModule.forRootAsync({ | ||
useFactory() { | ||
return { | ||
config | ||
}; | ||
} | ||
}) | ||
] | ||
}).compile(); | ||
}); | ||
|
||
test("provides a connection", () => { | ||
const connection = testingModule.get<knex>(KNEX_CONNECTION); | ||
|
||
expect(connection).toBeDefined(); | ||
}); | ||
|
||
test("provides a base model", () => { | ||
const model = testingModule.get<Model>(OBJECTION_BASE_MODEL); | ||
|
||
expect(model).toBeDefined(); | ||
}); | ||
}); | ||
|
||
describe("#createAsyncProviders", () => { | ||
class ModuleOptionsFactory implements ObjectionModuleOptionsFactory { | ||
public createObjectionModuleOptions(): ObjectionModuleOptions { | ||
return { | ||
config | ||
}; | ||
} | ||
} | ||
|
||
test("throws an error if options.useClass, useExisting, useFactory are not provided", () => { | ||
expect(() => { | ||
ObjectionCoreModule.createAsyncProviders({}); | ||
}).toThrowError("Invalid configuration"); | ||
}); | ||
|
||
test("leverages useClass if provided", () => { | ||
const providers = ObjectionCoreModule.createAsyncProviders({ | ||
useClass: ModuleOptionsFactory | ||
}); | ||
|
||
expect(providers).toEqual([ | ||
{ | ||
inject: [ModuleOptionsFactory], | ||
useFactory: expect.any(Function), | ||
provide: OBJECTION_MODULE_OPTIONS | ||
}, | ||
{ | ||
useClass: ModuleOptionsFactory, | ||
provide: ModuleOptionsFactory | ||
} | ||
]); | ||
}); | ||
|
||
test("returns an array of providers when useExisting is passed", () => { | ||
const providers = ObjectionCoreModule.createAsyncProviders({ | ||
useExisting: ModuleOptionsFactory | ||
}); | ||
|
||
expect(providers).toEqual([ | ||
{ | ||
inject: [ModuleOptionsFactory], | ||
useFactory: expect.any(Function), | ||
provide: OBJECTION_MODULE_OPTIONS | ||
} | ||
]); | ||
}); | ||
|
||
test("returns an array of providers when useFactory is passed", () => { | ||
const providers = ObjectionCoreModule.createAsyncProviders({ | ||
useFactory: () => ({ | ||
config | ||
}) | ||
}); | ||
|
||
expect(providers).toEqual([ | ||
{ | ||
inject: [], | ||
useFactory: expect.any(Function), | ||
provide: OBJECTION_MODULE_OPTIONS | ||
} | ||
]); | ||
}); | ||
}); | ||
|
||
describe("#createAsyncOptionsProvider", () => { | ||
// eslint-disable-next-line new-cap | ||
@Injectable() | ||
class ModuleOptionsFactory implements ObjectionModuleOptionsFactory { | ||
public createObjectionModuleOptions(): ObjectionModuleOptions { | ||
return { | ||
config | ||
}; | ||
} | ||
} | ||
|
||
test("returns the appropriate provider when useFactory is passed", () => { | ||
const provider = ObjectionCoreModule.createAsyncOptionsProvider({ | ||
useFactory: () => ({ | ||
config | ||
}) | ||
}); | ||
|
||
expect(provider).toEqual({ | ||
inject: [], | ||
provide: OBJECTION_MODULE_OPTIONS, | ||
useFactory: expect.any(Function) | ||
}); | ||
}); | ||
|
||
test("returns the appropriate provider when useExisting is passed", () => { | ||
const provider = ObjectionCoreModule.createAsyncOptionsProvider({ | ||
useExisting: ModuleOptionsFactory | ||
}); | ||
|
||
expect(provider).toEqual({ | ||
inject: [ModuleOptionsFactory], | ||
provide: OBJECTION_MODULE_OPTIONS, | ||
useFactory: expect.any(Function) | ||
}); | ||
}); | ||
|
||
test("returns an async factory function that calls createObjectionModuleOptions", async () => { | ||
jest.spyOn( | ||
ModuleOptionsFactory.prototype, | ||
"createObjectionModuleOptions" | ||
); | ||
|
||
await Test.createTestingModule({ | ||
imports: [ | ||
ObjectionCoreModule.forRootAsync({ | ||
useClass: ModuleOptionsFactory | ||
}) | ||
] | ||
}).compile(); | ||
|
||
expect( | ||
ModuleOptionsFactory.prototype.createObjectionModuleOptions | ||
).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
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