Skip to content

Commit

Permalink
chore: unit test the signal inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
profanis committed Mar 21, 2024
1 parent c07ac8c commit d92c833
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 60 deletions.
97 changes: 67 additions & 30 deletions projects/spectator/jest/test/set-input-alias-names.spec.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,72 @@
import { Component, Input } from '@angular/core';
import { Component, Input, input } from '@angular/core';
import { createComponentFactory } from '@ngneat/spectator/jest';

describe('SetInputAliasNames', () => {
@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('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');
});
});
});
97 changes: 67 additions & 30 deletions projects/spectator/test/set-input-alias-names.spec.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,72 @@
import { Component, Input } from '@angular/core';
import { Component, Input, input } from '@angular/core';
import { createComponentFactory } from '@ngneat/spectator';

describe('SetInputAliasNames', () => {
@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('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');
});
});
});

0 comments on commit d92c833

Please sign in to comment.