Coder Social home page Coder Social logo

Comments (12)

vespertilian avatar vespertilian commented on May 22, 2024 1

@ike18t Will do! Hopefully I can find some time this weekend.

from ng-mocks.

vespertilian avatar vespertilian commented on May 22, 2024 1

Yeah I had a great weekend, I was actually at a JavaScript camp. https://ix.campjs.com. So I figured it would be a good place to get the work done but every-time I tried to go to Github the 100+ other devs were also trying to download something, so I just gave up on the third timeout attempt.

I could have tried harder, but there were also waterfall walks and mountain vistas to checkout.

https://twitter.com/campjs

img_1037

from ng-mocks.

ike18t avatar ike18t commented on May 22, 2024 1

fe547af

from ng-mocks.

getsaf avatar getsaf commented on May 22, 2024

Thanks for the request, I have a little experience with Reactive Forms but I'm not following your question. Do you mind setting up a quick StackBlitz project to describe the issue? Don't worry about creating test in the StackBlitz, just a component that is representative of this issue so I can follow along with this feature request.

Thanks!

from ng-mocks.

vespertilian avatar vespertilian commented on May 22, 2024

Here it is

https://stackblitz.com/github/vespertilian/testing-reactive-forms-third-party-components

Might be easier to pull it and run it from the CLI

https://github.com/vespertilian/testing-reactive-forms-third-party-components

  it('should update the form when you fill in the type "select" field', () => {
    const {fixture, component} = setup();
    const typeSelect: MockMdSelectComponent = fixture.debugElement.query(By.css('mat-select')).componentInstance;

    typeSelect.propagateChange('Ninja');
    expect(component.warriorForm.value.type).toEqual('Ninja');
  });

Basically when testing forms I prefer to drive the form not just use for formController instance in the component controller as that does not tell you if you have bound to the form via "formControlName"

from ng-mocks.

vespertilian avatar vespertilian commented on May 22, 2024

@getsaf Just checking in, you are probably busy, which is fine. If there is anything else I can do to help move this issue along please let me know.

from ng-mocks.

ike18t avatar ike18t commented on May 22, 2024

@vespertilian What do you think about writeValue triggering the change callback? Would that solve your problem?

from ng-mocks.

vespertilian avatar vespertilian commented on May 22, 2024

Hey @ike18t, sorry for the late reply this slipped by me.

I had thought that as well initially, it's just as write value is normally used to set the value on the custom form control, I was concerned this will lead to unexpected behaviour, or hard to discover bugs.

I think this would work when you just grab the custom form control from the debug element and call writeValue.

However say you, or some other part of your test calls myFormControl.setValue(name: 'vespert'), if we bound it to writeValue on the form control, the value will be propagated back out, which would not usually happen as write value is normally only called when angular reactive formsl changes the value, so if you were say subscribing to value changes you would get a false output.

Also not sure if binding the input and output together could lead to some type of infinite feedback loop / weird change detection issues.

interface ControlValueAccessor {
  writeValue(obj: any): void
  registerOnChange(fn: any): void
  registerOnTouched(fn: any): void
  setDisabledState(isDisabled: boolean)?: void
}

# it's these two 'fn' we want to be able to mock out and manually activate 
registerOnChange(fn: any): void
registerOnTouched(fn: any): void

We could do it with a getter and setter on value and touched maybe?

formControl.value = 'vespert'

I still think

  propagateChange = (_: any) => {};
  setTouched = (_: any) => {};
  // or
  __propagateChange = (_: any) => {};
  __setTouched = (_: any) => {};

is the clearest and cleanest way to accomplish this, I would probably go with the __ just in case someone has a function on there custom form control called propagateChange.

from ng-mocks.

ike18t avatar ike18t commented on May 22, 2024

@vespertilian Fair enough. I don't have that much experience dealing with reactive forms so I'm going to have to lean on your experience. Feel free to submit a PR.

from ng-mocks.

vespertilian avatar vespertilian commented on May 22, 2024

@ike18t No internet this weekend, it will have to be some time next week!

from ng-mocks.

ike18t avatar ike18t commented on May 22, 2024

@vespertilian A weekend w/o internet? Are you OK? Did you survive?

from ng-mocks.

ike18t avatar ike18t commented on May 22, 2024

Wow! That is awesome!

from ng-mocks.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.