-
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.
Add unit test for Injector's defineConcerns() method
- Loading branch information
Showing
1 changed file
with
158 additions
and
0 deletions.
There are no files selected for viewing
158 changes: 158 additions & 0 deletions
158
tests/browser/packages/support/concerns/injector/defineConcerns.test.js
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,158 @@ | ||
import { AbstractConcern, AlreadyRegisteredError, InjectionError } from "@aedart/support/concerns"; | ||
import { CONCERN_CLASSES } from "@aedart/contracts/support/concerns"; | ||
import makeConcernsInjector from "../helpers/makeConcernsInjector"; | ||
|
||
describe('@aedart/support/concerns', () => { | ||
describe('ConcernsInjector', () => { | ||
describe('defineConcerns()', () => { | ||
|
||
it('can define concerns classes in target', () => { | ||
class ConcernA extends AbstractConcern {} | ||
class ConcernB extends AbstractConcern {} | ||
class ConcernC extends AbstractConcern {} | ||
|
||
class A {} | ||
|
||
const injector = makeConcernsInjector(A); | ||
|
||
injector.defineConcerns(A, [ | ||
ConcernA, | ||
ConcernB, | ||
ConcernC, | ||
]); | ||
|
||
// ------------------------------------------------------------------- // | ||
|
||
expect(Reflect.has(A, CONCERN_CLASSES)) | ||
.withContext('Target does not have static concern classes property defined') | ||
.toBeTrue(); | ||
|
||
const registered = A[CONCERN_CLASSES]; | ||
expect(Array.isArray(registered)) | ||
.withContext('static concern classes property is not an array') | ||
.toBeTrue(); | ||
|
||
expect(registered) | ||
.withContext('incorrect concern classes registered') | ||
.toEqual([ ConcernA, ConcernB, ConcernC ]); | ||
}); | ||
|
||
it('inherits already registered concerns from parent', () => { | ||
class ConcernA extends AbstractConcern {} | ||
class ConcernB extends AbstractConcern {} | ||
class ConcernC extends AbstractConcern {} | ||
|
||
class A {} | ||
|
||
class B extends A {} | ||
|
||
const injector = makeConcernsInjector(A); | ||
|
||
// Define for A | ||
injector.defineConcerns(A, [ | ||
ConcernA, | ||
ConcernB | ||
]); | ||
|
||
// Define for B | ||
injector.defineConcerns(B, [ | ||
ConcernC | ||
]); | ||
|
||
// ------------------------------------------------------------------- // | ||
|
||
expect(Reflect.has(B, CONCERN_CLASSES)) | ||
.withContext('Target B does not have static concern classes property defined') | ||
.toBeTrue(); | ||
|
||
const registryB = B[CONCERN_CLASSES]; | ||
|
||
// Debug | ||
// console.log('registry b', registryB); | ||
|
||
expect(registryB) | ||
.withContext('Target B did not inherit concern classes from parent class') | ||
.toEqual([ ConcernA, ConcernB, ConcernC ]); | ||
|
||
// ------------------------------------------------------------------- // | ||
|
||
// Ensure that A's concern classes have not been altered | ||
const registryA = A[CONCERN_CLASSES]; | ||
|
||
// Debug | ||
// console.log('registry a', registryA); | ||
|
||
expect(registryA) | ||
.withContext('Target A concern classes has been modified - BUT SHOULD NOT BE!') | ||
.toEqual([ ConcernA, ConcernB ]); | ||
}); | ||
|
||
it('fails when attempting to register same concern twice in target', () => { | ||
class ConcernA extends AbstractConcern {} | ||
|
||
class A {} | ||
|
||
const injector = makeConcernsInjector(A); | ||
|
||
const callback = () => { | ||
injector.defineConcerns(A, [ | ||
ConcernA, | ||
ConcernA, // Should cause error | ||
]); | ||
} | ||
|
||
// ------------------------------------------------------------------- // | ||
|
||
expect(callback) | ||
.toThrowError(AlreadyRegisteredError); | ||
}); | ||
|
||
it('fails to register concern class when already registered in parent', () => { | ||
class ConcernA extends AbstractConcern {} | ||
|
||
class A {} | ||
|
||
class B extends A {} | ||
|
||
const injector = makeConcernsInjector(A); | ||
|
||
// Define for A | ||
injector.defineConcerns(A, [ | ||
ConcernA | ||
]); | ||
|
||
// Define for B | ||
const callback = () => { | ||
injector.defineConcerns(B, [ | ||
ConcernA // Should cause error | ||
]); | ||
} | ||
|
||
// ------------------------------------------------------------------- // | ||
|
||
expect(callback) | ||
.toThrowError(AlreadyRegisteredError); | ||
}); | ||
|
||
it('fails if unable to define static concern classes property', () => { | ||
class ConcernA extends AbstractConcern {} | ||
|
||
class A {} | ||
Object.freeze(A); // This can prevent static property from being added | ||
|
||
const injector = makeConcernsInjector(A); | ||
|
||
const callback = () => { | ||
injector.defineConcerns(A, [ | ||
ConcernA | ||
]); | ||
} | ||
|
||
// ------------------------------------------------------------------- // | ||
|
||
expect(callback) | ||
.toThrowError(InjectionError); | ||
}); | ||
}); | ||
}); | ||
}); |