-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mock-render): throws on wrong usage #488
- Loading branch information
Showing
15 changed files
with
309 additions
and
170 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
49 changes: 49 additions & 0 deletions
49
libs/ng-mocks/src/lib/mock-render/func.reflect-template.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,49 @@ | ||
import { Component, Directive } from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { extendClass } from '../common/core.helpers'; | ||
import coreReflectDirectiveResolve from '../common/core.reflect.directive-resolve'; | ||
import { AnyType } from '../common/core.types'; | ||
import { isNgDef } from '../common/func.is-ng-def'; | ||
|
||
const registerTemplateMiddleware = (template: AnyType<any>, meta: Directive): void => { | ||
const child = extendClass(template); | ||
|
||
let providers = meta.providers || []; | ||
providers = [ | ||
...providers, | ||
{ | ||
provide: template, | ||
useExisting: child, | ||
}, | ||
]; | ||
meta.providers = providers; | ||
|
||
if (isNgDef(template, 'c')) { | ||
Component(meta)(child); | ||
} else { | ||
Directive(meta)(child); | ||
} | ||
TestBed.configureTestingModule({ | ||
declarations: [child], | ||
}); | ||
}; | ||
|
||
export default (template: AnyType<any>): Directive => { | ||
if (!isNgDef(template, 'c') && !isNgDef(template, 'd')) { | ||
return {}; | ||
} | ||
|
||
const meta = { ...coreReflectDirectiveResolve(template) }; | ||
|
||
if (meta.selector && meta.selector.match(/[\[\],]/)) { | ||
meta.selector = ''; | ||
} | ||
|
||
if (!meta.selector) { | ||
meta.selector = `ng-mocks-${template.name}`; | ||
registerTemplateMiddleware(template, meta); | ||
} | ||
|
||
return meta; | ||
}; |
Oops, something went wrong.