-
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: one step closer to serializer release
- Loading branch information
1 parent
5c19581
commit 65452ce
Showing
24 changed files
with
326 additions
and
43 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
2 changes: 1 addition & 1 deletion
2
__tests__/utils/buildSymbol.test.ts → ...ts__/decorators/utils/buildSymbol.test.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
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
88 changes: 88 additions & 0 deletions
88
__tests__/serializers/utils/assertMetadataIsPresent.test.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,88 @@ | ||
import { assertMetadataIsPresent } from '../../../src/serializers/utils/assertMetadataIsPresent'; | ||
|
||
describe('`assertMetadataIsPresent`', () => { | ||
it('should throw an error if `Symbol.metadata` is undefined', () => { | ||
const originalSymbolMetadata = Symbol.metadata; | ||
|
||
// @ts-expect-error | ||
Symbol.metadata = undefined; | ||
|
||
try { | ||
assertMetadataIsPresent({}); | ||
} catch (error) { | ||
expect((error as Error).message).toEqual( | ||
'Failed to assert the presence of metadata because the metadata symbol is undefined. You may need to import the `@tsmetadata/polyfill` package.', | ||
); | ||
} | ||
|
||
// @ts-expect-error | ||
Symbol.metadata = originalSymbolMetadata; | ||
}); | ||
|
||
describe('when the candidate is an array of objects', () => { | ||
it('should throw an error if at least one candidate is not an object', () => { | ||
const candidate = [1, {}]; | ||
|
||
try { | ||
assertMetadataIsPresent(candidate); | ||
} catch (error) { | ||
expect((error as Error).message).toEqual( | ||
'Failed to assert the presence of metadata because at least one candidate is not an object, meaning no constructor is present.', | ||
); | ||
} | ||
}); | ||
|
||
it('should throw an error if no metadata is found on an object constructor', () => { | ||
const candidate = [{}, {}]; | ||
|
||
try { | ||
assertMetadataIsPresent(candidate); | ||
} catch (error) { | ||
expect((error as Error).message).toEqual( | ||
'No metadata was found on an object constructor.', | ||
); | ||
} | ||
}); | ||
|
||
it('should return `true` if metadata is found on all object constructors', () => { | ||
const candidate = [ | ||
{ constructor: { [Symbol.metadata]: 'metadata' } }, | ||
{ constructor: { [Symbol.metadata]: 'metadata' } }, | ||
]; | ||
|
||
expect(assertMetadataIsPresent(candidate)).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('when the candidate is an object', () => { | ||
it('should throw an error if the candidate is not an object', () => { | ||
const candidate = 1; | ||
|
||
try { | ||
assertMetadataIsPresent(candidate); | ||
} catch (error) { | ||
expect((error as Error).message).toEqual( | ||
'Failed to assert the presence of metadata because at least one candidate is not an object, meaning no constructor is present.', | ||
); | ||
} | ||
}); | ||
|
||
it('should throw an error if no metadata is found on the object constructor', () => { | ||
const candidate = {}; | ||
|
||
try { | ||
assertMetadataIsPresent(candidate); | ||
} catch (error) { | ||
expect((error as Error).message).toEqual( | ||
'No metadata was found on an object constructor.', | ||
); | ||
} | ||
}); | ||
|
||
it('should return `true` if metadata is found on the object constructor', () => { | ||
const candidate = { constructor: { [Symbol.metadata]: 'metadata' } }; | ||
|
||
expect(assertMetadataIsPresent(candidate)).toBe(true); | ||
}); | ||
}); | ||
}); |
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,43 @@ | ||
import { Chance } from 'chance'; | ||
import { assertMetadataIsPresent } from '../../../src/serializers/utils/assertMetadataIsPresent'; | ||
import { getMetadataBySymbol } from '../../../src/serializers/utils/getMetadataBySymbol'; | ||
|
||
jest.mock('../../../src/serializers/utils/assertMetadataIsPresent'); | ||
const assertMetadataIsPresentMocked = jest.mocked(assertMetadataIsPresent); | ||
|
||
describe('`getMetadataSymbol`', () => { | ||
let chance: Chance.Chance; | ||
|
||
beforeEach(() => { | ||
chance = new Chance(); | ||
}); | ||
|
||
it('should assert metadata is present in the given object', () => { | ||
const object = { | ||
constructor: { | ||
[Symbol.metadata]: {}, | ||
}, | ||
}; | ||
|
||
getMetadataBySymbol(object, Symbol('test')); | ||
|
||
expect(assertMetadataIsPresentMocked).toHaveBeenCalledTimes(1); | ||
expect(assertMetadataIsPresentMocked).toHaveBeenCalledWith(object); | ||
}); | ||
|
||
it("should return the symbol's metadata on the given object", () => { | ||
const symbol = Symbol(chance.string()); | ||
|
||
const object = { | ||
constructor: { | ||
[Symbol.metadata]: { | ||
[symbol]: chance.string(), | ||
}, | ||
}, | ||
}; | ||
|
||
const metadata = getMetadataBySymbol(object, symbol); | ||
|
||
expect(metadata).toBe(object.constructor[Symbol.metadata][symbol]); | ||
}); | ||
}); |
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 @@ | ||
import { isObject } from '../../../src/serializers/utils/isObject'; | ||
|
||
describe('`isObject`', () => { | ||
it('should return `false` when the value is `null`', () => { | ||
expect(isObject(null)).toBe(false); | ||
}); | ||
|
||
it('should return `false` when the value is not an object', () => { | ||
expect(isObject('')).toBe(false); | ||
}); | ||
|
||
it('should return `true` when the value is an object', () => { | ||
expect(isObject({})).toBe(true); | ||
}); | ||
}); |
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,13 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/tsconfig", | ||
"extends": "@tsmetadata/polyfill/lib/tsconfig.polyfill.json", | ||
"compilerOptions": { | ||
"target": "ES2022", | ||
"module": "NodeNext", | ||
"moduleResolution": "NodeNext", | ||
"esModuleInterop": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"strict": true | ||
}, | ||
"include": ["./**/*.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
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,5 @@ | ||
import '@tsmetadata/polyfill'; | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); |
Oops, something went wrong.