-
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(spectator): set input accepts alias names (#652)
* fix(spectator): set input accepts alias names * chore: unit test the signal inputs
- Loading branch information
Showing
3 changed files
with
149 additions
and
4 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
projects/spectator/jest/test/set-input-alias-names.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,72 @@ | ||
import { Component, Input, input } from '@angular/core'; | ||
import { createComponentFactory } from '@ngneat/spectator/jest'; | ||
|
||
describe('SetInputAliasNames', () => { | ||
describe('input decorators', () => { | ||
@Component({ | ||
selector: 'app-root', | ||
template: ` | ||
<div data-test="set-input--name">{{ name }}</div> | ||
<div data-test="set-input--age">{{ numOfYears }}</div> | ||
`, | ||
standalone: true, | ||
}) | ||
class DummyComponent { | ||
@Input('userName') name!: string; | ||
@Input({ alias: 'age' }) numOfYears!: number; | ||
} | ||
|
||
const createComponent = createComponentFactory(DummyComponent); | ||
|
||
it('setInput should respect the alias names', () => { | ||
// Arrange | ||
const spectator = createComponent(); | ||
|
||
const nameElement = spectator.query('[data-test="set-input--name"]')!; | ||
const ageElement = spectator.query('[data-test="set-input--age"]')!; | ||
|
||
// Act | ||
spectator.setInput('userName', 'John'); | ||
spectator.setInput('age', '123'); | ||
|
||
// Assert | ||
expect(nameElement.innerHTML).toBe('John'); | ||
expect(ageElement.innerHTML).toBe('123'); | ||
}); | ||
}); | ||
|
||
describe('signal inputs', () => { | ||
@Component({ | ||
selector: 'app-root', | ||
template: ` | ||
<div data-test="set-input--name">{{ name() }}</div> | ||
<div data-test="set-input--age">{{ numOfYears() }}</div> | ||
`, | ||
standalone: true, | ||
}) | ||
class DummyComponent { | ||
name = input.required({ alias: 'userName' }); | ||
numOfYears = input(0, { alias: 'age' }); | ||
} | ||
|
||
const createComponent = createComponentFactory(DummyComponent); | ||
|
||
it('setInput should respect the alias names', () => { | ||
// Arrange | ||
const spectator = createComponent({ | ||
detectChanges: false, | ||
}); | ||
|
||
const nameElement = spectator.query('[data-test="set-input--name"]')!; | ||
const ageElement = spectator.query('[data-test="set-input--age"]')!; | ||
|
||
// Act | ||
spectator.setInput('userName', 'John'); | ||
spectator.setInput('age', '123'); | ||
|
||
// Assert | ||
expect(nameElement.innerHTML).toBe('John'); | ||
expect(ageElement.innerHTML).toBe('123'); | ||
}); | ||
}); | ||
}); |
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,72 @@ | ||
import { Component, Input, input } from '@angular/core'; | ||
import { createComponentFactory } from '@ngneat/spectator'; | ||
|
||
describe('SetInputAliasNames', () => { | ||
describe('input decorators', () => { | ||
@Component({ | ||
selector: 'app-root', | ||
template: ` | ||
<div data-test="set-input--name">{{ name }}</div> | ||
<div data-test="set-input--age">{{ numOfYears }}</div> | ||
`, | ||
standalone: true, | ||
}) | ||
class DummyComponent { | ||
@Input('userName') name!: string; | ||
@Input({ alias: 'age' }) numOfYears!: number; | ||
} | ||
|
||
const createComponent = createComponentFactory(DummyComponent); | ||
|
||
it('setInput should respect the alias names', () => { | ||
// Arrange | ||
const spectator = createComponent(); | ||
|
||
const nameElement = spectator.query('[data-test="set-input--name"]')!; | ||
const ageElement = spectator.query('[data-test="set-input--age"]')!; | ||
|
||
// Act | ||
spectator.setInput('userName', 'John'); | ||
spectator.setInput('age', '123'); | ||
|
||
// Assert | ||
expect(nameElement.innerHTML).toBe('John'); | ||
expect(ageElement.innerHTML).toBe('123'); | ||
}); | ||
}); | ||
|
||
describe('signal inputs', () => { | ||
@Component({ | ||
selector: 'app-root', | ||
template: ` | ||
<div data-test="set-input--name">{{ name() }}</div> | ||
<div data-test="set-input--age">{{ numOfYears() }}</div> | ||
`, | ||
standalone: true, | ||
}) | ||
class DummyComponent { | ||
name = input.required({ alias: 'userName' }); | ||
numOfYears = input(0, { alias: 'age' }); | ||
} | ||
|
||
const createComponent = createComponentFactory(DummyComponent); | ||
|
||
it('setInput should respect the alias names', () => { | ||
// Arrange | ||
const spectator = createComponent({ | ||
detectChanges: false, | ||
}); | ||
|
||
const nameElement = spectator.query('[data-test="set-input--name"]')!; | ||
const ageElement = spectator.query('[data-test="set-input--age"]')!; | ||
|
||
// Act | ||
spectator.setInput('userName', 'John'); | ||
spectator.setInput('age', '123'); | ||
|
||
// Assert | ||
expect(nameElement.innerHTML).toBe('John'); | ||
expect(ageElement.innerHTML).toBe('123'); | ||
}); | ||
}); | ||
}); |