Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Radiobutton group] check won't work if uncheck hasn't been called on other options #154

Closed
psantus opened this issue Oct 29, 2019 · 4 comments

Comments

@psantus
Copy link

psantus commented Oct 29, 2019

My test case: I have a radiobutton group, with two options:
"allStatus" or "inProgressStatus"

The following test will fail

    it('should update criteria model when changing criteria', () => {
      // Given
      loadTester();

      // When
      tester.inProgressStatus.check();
      tester.allStatus.uncheck();

      // Then
      expect(tester.componentInstance.form.getRawValue()).toEqual({
        inProgress: 'true'
      });
    });

while the following (just inverting uncheck/check) will pass

    it('should update criteria model when changing criteria', () => {
      // Given
      loadTester();

      // When
      tester.allStatus.uncheck();
      tester.inProgressStatus.check();

      // Then
      expect(tester.componentInstance.form.getRawValue()).toEqual({
        inProgress: 'true'
      });
    });

(thanks for this great library by the way)

@cexbrayat
Copy link
Member

👋 Hi Paul!

Could you provide a small repro or the component with unit test that reproduces the issue so we can investigate?

I suspect it might be more from how you set up your test than something weird with ngx-speculoos itself, as it just checks the input and emits the change event as you can see:

/**
* Checks the wrapped input, then dispatches an event of type change and triggers a change detection
*/
check() {
this.nativeElement.checked = true;
this.dispatchEventOfType('change');
}
/**
* Unchecks the wrapped input, then dispatches an event of type change and triggers a change detection
*/
uncheck() {
this.nativeElement.checked = false;
this.dispatchEventOfType('change');
}

@psantus
Copy link
Author

psantus commented Oct 29, 2019

Hi Cedric

Thanks for your responsiveness!

If you still have access to our repo, I slacked you a link to the affected file.

Not sure how much I should extract to give you the relevant info

@cexbrayat
Copy link
Member

I don't think I have. You can create a new CLI application with just one component and a failing test reproducing the issue (I'm sure someone from the team can help you :) ) and push the application to Github, and we'll take a look.

@jnizet
Copy link
Member

jnizet commented Aug 15, 2020

Suspecting that this is about ng-bootstrap buttons, I tried to reproduce the issue using the following complete minimal example, but could not: the tests pass fine.

Closing due to lack of feedback.

import { Component } from '@angular/core';
import { FormBuilder, FormGroup, ReactiveFormsModule } from '@angular/forms';
import { TestBed } from '@angular/core/testing';
import { ComponentTester } from 'ngx-speculoos';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';

@Component({
  template: `
    <form [formGroup]="form">
      <div class="btn-group btn-group-toggle" ngbRadioGroup name="radioBasic" formControlName="foo">
        <label ngbButtonLabel class="btn-primary">
          <input id="a" ngbButton type="radio" value="a"> A
        </label>
        <label ngbButtonLabel class="btn-primary">
          <input id="b" ngbButton type="radio" value="b"> B
        </label>
      </div>
    </form>
  `
})
class TestComponent {
  form: FormGroup;

  constructor(fb: FormBuilder) {
    this.form = fb.group({
      foo: ['a']
    });
  }
}

describe('test', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [ReactiveFormsModule, NgbModule],
      declarations: [TestComponent]
    });
  })

  it('should work', () => {
    const tester = new ComponentTester<TestComponent>(TestComponent);
    tester.detectChanges();

    expect(tester.input('#a')).toBeChecked();
    expect(tester.input('#b')).not.toBeChecked();

    tester.input('#b').check();

    expect(tester.input('#a')).not.toBeChecked();
    expect(tester.input('#b')).toBeChecked();
  });
});

@jnizet jnizet closed this as completed Aug 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants