forked from nartc/mapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(classes): add more tests to classes plugin
- Loading branch information
Showing
20 changed files
with
711 additions
and
47 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
165 changes: 165 additions & 0 deletions
165
packages/classes/src/lib/utils/specs/instantiate.util.spec.ts
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,165 @@ | ||
// eslint-disable-next-line @nrwl/nx/enforce-module-boundaries | ||
import { | ||
createSpyObject, | ||
resetAllWhenMocks, | ||
when, | ||
} from '@automapper/test-util'; | ||
import { ClassInstanceStorage, ClassMetadataStorage } from '../../storages'; | ||
import { instantiate } from '../instantiate.util'; | ||
|
||
describe('instantiate', () => { | ||
const mockedInstanceStorage = createSpyObject(ClassInstanceStorage, { | ||
resetAllCount: jest.fn(), | ||
getDepthAndCount: jest.fn(), | ||
resetCount: jest.fn(), | ||
setCount: jest.fn(), | ||
}); | ||
|
||
const mockedMetadataStorage = createSpyObject(ClassMetadataStorage, { | ||
getMetadata: jest.fn(), | ||
}); | ||
|
||
class Bar { | ||
bar: string; | ||
date: Date; | ||
} | ||
|
||
class Foo { | ||
foo: string; | ||
bar: Bar; | ||
} | ||
|
||
const defaultDate = new Date('10/14/1991'); | ||
const defaultFoo: Foo = new Foo(); | ||
defaultFoo.foo = 'foo'; | ||
defaultFoo.bar = new Bar(); | ||
defaultFoo.bar.bar = 'bar'; | ||
defaultFoo.bar.date = defaultDate; | ||
|
||
let parameterizedInstantiate: () => [Foo, unknown[]?]; | ||
|
||
describe('without default value', () => { | ||
beforeEach(() => { | ||
parameterizedInstantiate = () => | ||
instantiate(mockedInstanceStorage, mockedMetadataStorage, Foo); | ||
}); | ||
|
||
afterEach(() => { | ||
resetAllWhenMocks(); | ||
}); | ||
|
||
it('should return raw instance if meta is empty', () => { | ||
mockEmpty(); | ||
|
||
const result = parameterizedInstantiate(); | ||
expect(result).toEqual([new Foo()]); | ||
}); | ||
|
||
it('should return proper instance with metadata', () => { | ||
mockWithoutDepth(); | ||
|
||
const fooInstance = new Foo(); | ||
fooInstance.foo = undefined; | ||
fooInstance.bar = new Bar(); | ||
|
||
const result = parameterizedInstantiate(); | ||
expect(result).toEqual([fooInstance, [['bar', Bar]]]); | ||
}); | ||
|
||
it('should return proper instance with depth', () => { | ||
mockWithDepth(); | ||
|
||
const fooInstance = new Foo(); | ||
fooInstance.foo = undefined; | ||
fooInstance.bar = new Bar(); | ||
fooInstance.bar.bar = undefined; | ||
fooInstance.bar.date = defaultDate; | ||
|
||
const result = parameterizedInstantiate(); | ||
result[0].bar.date = defaultDate; | ||
expect(result).toEqual([fooInstance, [['bar', Bar]]]); | ||
}); | ||
}); | ||
|
||
describe('with default value', () => { | ||
beforeEach(() => { | ||
parameterizedInstantiate = () => | ||
instantiate( | ||
mockedInstanceStorage, | ||
mockedMetadataStorage, | ||
Foo, | ||
defaultFoo | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
resetAllWhenMocks(); | ||
}); | ||
|
||
it('should return raw instance if meta is empty', () => { | ||
mockEmpty(); | ||
|
||
const result = parameterizedInstantiate(); | ||
expect(result).toEqual([defaultFoo]); | ||
}); | ||
|
||
it('should return proper instance with metadata', () => { | ||
mockWithoutDepth(); | ||
|
||
const result = parameterizedInstantiate(); | ||
expect(result).toEqual([defaultFoo, [['bar', Bar]]]); | ||
}); | ||
|
||
it('should return proper instance with depth', () => { | ||
mockWithDepth(); | ||
|
||
const result = parameterizedInstantiate(); | ||
expect(result).toEqual([defaultFoo, [['bar', Bar]]]); | ||
}); | ||
}); | ||
|
||
function mockEmpty() { | ||
when(mockedInstanceStorage.getDepthAndCount) | ||
.calledWith(Foo, 'bar') | ||
.mockReturnValueOnce([0, 0]); | ||
when(mockedMetadataStorage.getMetadata) | ||
.calledWith(Foo) | ||
.mockReturnValueOnce([]); | ||
} | ||
|
||
function mockWithoutDepth() { | ||
when(mockedInstanceStorage.getDepthAndCount) | ||
.calledWith(Foo, 'bar') | ||
.mockReturnValueOnce([0, 0]); | ||
when(mockedMetadataStorage.getMetadata) | ||
.calledWith(Foo) | ||
.mockReturnValueOnce([ | ||
['foo', () => String], | ||
['bar', () => Bar], | ||
]); | ||
when(mockedMetadataStorage.getMetadata) | ||
.calledWith(Bar) | ||
.mockReturnValueOnce([ | ||
['bar', () => String], | ||
['date', () => Date], | ||
]); | ||
} | ||
|
||
function mockWithDepth() { | ||
when(mockedInstanceStorage.getDepthAndCount) | ||
.calledWith(Foo, 'bar') | ||
.mockReturnValueOnce([1, 0]); | ||
when(mockedMetadataStorage.getMetadata) | ||
.calledWith(Foo) | ||
.mockReturnValueOnce([ | ||
['foo', () => String], | ||
['bar', () => Bar], | ||
]); | ||
when(mockedMetadataStorage.getMetadata) | ||
.calledWith(Bar) | ||
.mockReturnValueOnce([ | ||
['bar', () => String], | ||
['date', () => Date], | ||
]); | ||
} | ||
}); |
28 changes: 28 additions & 0 deletions
28
packages/classes/src/lib/utils/specs/is-class.util.spec.ts
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,28 @@ | ||
import { isClass } from '../is-class.util'; | ||
|
||
describe('isClass', () => { | ||
it('should work', () => { | ||
expect(isClass(String)).toEqual(true); | ||
expect(isClass(Number)).toEqual(true); | ||
expect( | ||
isClass(function something() { | ||
console.log(''); | ||
}) | ||
).toEqual(true); | ||
expect( | ||
isClass(() => { | ||
console.log(''); | ||
}) | ||
).toEqual(false); | ||
|
||
expect( | ||
isClass(function () { | ||
console.log(''); | ||
}) | ||
).toEqual(false); | ||
|
||
class Foo {} | ||
|
||
expect(isClass(Foo)).toEqual(true); | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
packages/classes/src/lib/utils/specs/is-date-constructor.util.spec.ts
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,9 @@ | ||
import { isDateConstructor } from '../is-date-constructor.util'; | ||
|
||
describe('isDateConstructor', () => { | ||
it('should work', () => { | ||
expect(isDateConstructor(Date)).toEqual(true); | ||
expect(isDateConstructor(String)).toEqual(false); | ||
expect(isDateConstructor(Number)).toEqual(false); | ||
}); | ||
}); |
10 changes: 10 additions & 0 deletions
10
packages/classes/src/lib/utils/specs/is-primitive-constructor.util.spec.ts
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 { isPrimitiveConstructor } from '../is-primitive-constructor.util'; | ||
|
||
describe('isPrimitiveConstructor', () => { | ||
it('should work', () => { | ||
expect(isPrimitiveConstructor(String)).toEqual(true); | ||
expect(isPrimitiveConstructor(Number)).toEqual(true); | ||
expect(isPrimitiveConstructor(Boolean)).toEqual(true); | ||
expect(isPrimitiveConstructor(Date)).toEqual(false); | ||
}); | ||
}); |
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 @@ | ||
{ "extends": "../../.eslintrc.json", "ignorePatterns": ["!**/*"], "rules": {} } |
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,7 @@ | ||
# test-util | ||
|
||
This library was generated with [Nx](https://nx.dev). | ||
|
||
## Running unit tests | ||
|
||
Run `nx test test-util` to execute the unit tests via [Jest](https://jestjs.io). |
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,15 @@ | ||
module.exports = { | ||
displayName: 'test-util', | ||
preset: '../../jest.preset.js', | ||
globals: { | ||
'ts-jest': { | ||
tsConfig: '<rootDir>/tsconfig.spec.json', | ||
}, | ||
}, | ||
testEnvironment: 'node', | ||
transform: { | ||
'^.+\\.[tj]sx?$': 'ts-jest', | ||
}, | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], | ||
coverageDirectory: '../../coverage/packages/test-util', | ||
}; |
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,2 @@ | ||
export * from './lib/create-spy-object'; | ||
export * from './lib/when'; |
Oops, something went wrong.