-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Angular: Provide alternative implementation of checking the instance …
…of a decorator
- Loading branch information
1 parent
202d918
commit 418ff82
Showing
6 changed files
with
88 additions
and
61 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
50 changes: 50 additions & 0 deletions
50
code/frameworks/angular/src/client/angular-beta/utils/isDecoratorInstanceOf.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,50 @@ | ||
import { Component, Directive, Pipe, Input, Output, NgModule } from '@angular/core'; | ||
import { isDecoratorInstanceOf } from './isDecoratorInstanceOf'; | ||
|
||
// Simulate Angular's behavior by manually adding the ngMetadataName property, since this information is added during compile time. | ||
const MockComponentDecorator = { ...Component, ngMetadataName: 'Component' }; | ||
const MockDirectiveDecorator = { ...Directive, ngMetadataName: 'Directive' }; | ||
const MockPipeDecorator = { ...Pipe, ngMetadataName: 'Pipe' }; | ||
const MockInputDecorator = { ...Input, ngMetadataName: 'Input' }; | ||
const MockOutputDecorator = { ...Output, ngMetadataName: 'Output' }; | ||
const MockNgModuleDecorator = { ...NgModule, ngMetadataName: 'NgModule' }; | ||
|
||
describe('isDecoratorInstanceOf', () => { | ||
it('should correctly identify a Component', () => { | ||
expect(isDecoratorInstanceOf(MockComponentDecorator, 'Component')).toBe(true); | ||
}); | ||
|
||
it('should correctly identify a Directive', () => { | ||
expect(isDecoratorInstanceOf(MockDirectiveDecorator, 'Directive')).toBe(true); | ||
}); | ||
|
||
it('should correctly identify a Pipe', () => { | ||
expect(isDecoratorInstanceOf(MockPipeDecorator, 'Pipe')).toBe(true); | ||
}); | ||
|
||
it('should correctly identify an Input', () => { | ||
expect(isDecoratorInstanceOf(MockInputDecorator, 'Input')).toBe(true); | ||
}); | ||
|
||
it('should correctly identify an Output', () => { | ||
expect(isDecoratorInstanceOf(MockOutputDecorator, 'Output')).toBe(true); | ||
}); | ||
|
||
it('should correctly identify an NgModule', () => { | ||
expect(isDecoratorInstanceOf(MockNgModuleDecorator, 'NgModule')).toBe(true); | ||
}); | ||
|
||
it('should return false for mismatched metadata names', () => { | ||
expect(isDecoratorInstanceOf(MockComponentDecorator, 'Directive')).toBe(false); | ||
}); | ||
|
||
it('should handle null or undefined decorators gracefully', () => { | ||
expect(isDecoratorInstanceOf(null, 'Component')).toBe(false); | ||
expect(isDecoratorInstanceOf(undefined, 'Component')).toBe(false); | ||
}); | ||
|
||
it('should handle decorators without ngMetadataName property', () => { | ||
const mockDecoratorWithoutMetadata = {}; | ||
expect(isDecoratorInstanceOf(mockDecoratorWithoutMetadata, 'Component')).toBe(false); | ||
}); | ||
}); |
6 changes: 6 additions & 0 deletions
6
code/frameworks/angular/src/client/angular-beta/utils/isDecoratorInstanceOf.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,6 @@ | ||
export function isDecoratorInstanceOf( | ||
decorator: any, | ||
name: 'Component' | 'Directive' | 'Pipe' | 'Input' | 'Output' | 'NgModule' | ||
) { | ||
return decorator?.ngMetadataName === name; | ||
} |