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

feat(ngMock): allow mock $controller service to set up controller bindings #11239

Closed
wants to merge 1 commit into
base: master
from

Conversation

Projects
None yet
7 participants
@caitp
Contributor

caitp commented Mar 4, 2015

Adds a new mock for the $controller service, in order to simplify testing using the
bindToController feature.

var dictionaryOfControllerBindings = {
  data: [
    { id: 0, phone: '...', name: '...' },
    { id: 1, phone: '...', name: '...' },
  ]
};

// When the MyCtrl constructor is called, `this.data ~= dictionaryOfControllerBindings.data`
$controller(MyCtrl, myLocals, dictionaryOfControllerBindings);

Closes #9425

@googlebot googlebot added the cla: yes label Mar 4, 2015

@caitp

This comment has been minimized.

Show comment
Hide comment
@caitp

caitp Mar 4, 2015

Contributor

So:

Should it be a shallow copy, as extend() currently does? This works better if the dictionary contains native values.

Contributor

caitp commented Mar 4, 2015

So:

Should it be a shallow copy, as extend() currently does? This works better if the dictionary contains native values.

feat(ngMock): allow mock $controller service to set up controller bin…
…dings

Adds a new mock for the $controller service, in order to simplify testing using the
bindToController feature.

```js
var dictionaryOfControllerBindings = {
  data: [
    { id: 0, phone: '...', name: '...' },
    { id: 1, phone: '...', name: '...' },
  ]
};

// When the MyCtrl constructor is called, `this.data ~= dictionaryOfControllerBindings.data`
$controller(MyCtrl, myLocals, dictionaryOfControllerBindings);
```

Closes #9425
* @param {Object} locals Injection locals for Controller.
* @param {Object=} bindings Properties to add to the controller before invoking
* the constructor. This is used to simulate the `bindToController` feature
* and simplify certain kinds oftests.

This comment has been minimized.

@gkalpak

gkalpak Mar 5, 2015

Member

oftests --> of tests

@gkalpak

gkalpak Mar 5, 2015

Member

oftests --> of tests

@caitp

This comment has been minimized.

Show comment
Hide comment
@caitp

caitp Mar 5, 2015

Contributor

@SonofNun15 / @cesarandreu would an API like this work for you guys? or do you prefer having the actual bindings set up

Contributor

caitp commented Mar 5, 2015

@SonofNun15 / @cesarandreu would an API like this work for you guys? or do you prefer having the actual bindings set up

@SonofNun15

This comment has been minimized.

Show comment
Hide comment
@SonofNun15

SonofNun15 Mar 5, 2015

That looks like exactly what I was hoping for. Bindings are not required from my perspective. Thanks so much!

SonofNun15 commented Mar 5, 2015

That looks like exactly what I was hoping for. Bindings are not required from my perspective. Thanks so much!

@cesarandreu

This comment has been minimized.

Show comment
Hide comment
@cesarandreu

cesarandreu Mar 5, 2015

@caitp this works for me :D. I don't need bindings.

I've basically gotten rid of big link functions in my directives. In my link function I'll setup watchers and call controller actions, and handle any kind of DOM transformation in as much isolation as possible. The controller is left responsible for tracking state and providing glue to services.

cesarandreu commented Mar 5, 2015

@caitp this works for me :D. I don't need bindings.

I've basically gotten rid of big link functions in my directives. In my link function I'll setup watchers and call controller actions, and handle any kind of DOM transformation in as much isolation as possible. The controller is left responsible for tracking state and providing glue to services.

@caitp

This comment has been minimized.

Show comment
Hide comment
@caitp

caitp Mar 5, 2015

Contributor

okay --- @petebacondarwin if you aren't busy watching the conference, does this look good to you with the typo fixed?

Contributor

caitp commented Mar 5, 2015

okay --- @petebacondarwin if you aren't busy watching the conference, does this look good to you with the typo fixed?

module(function($controllerProvider) {
$controllerProvider.register('testCtrl', function() {
called = true;
expect(this.data).toEqual(data);

This comment has been minimized.

@petebacondarwin

petebacondarwin Mar 6, 2015

Member

Do we want to be more strict and say toBe here? I know it doesn't really matter but since you asked the question about shallow copying I guess we could make it clear what the result of that was.

@petebacondarwin

petebacondarwin Mar 6, 2015

Member

Do we want to be more strict and say toBe here? I know it doesn't really matter but since you asked the question about shallow copying I guess we could make it clear what the result of that was.

@petebacondarwin

This comment has been minimized.

Show comment
Hide comment
@petebacondarwin

petebacondarwin Mar 6, 2015

Member

LGTM, merging with tweaked docs. Do we want to change the unit test WRT #11239 (comment) ?

Member

petebacondarwin commented Mar 6, 2015

LGTM, merging with tweaked docs. Do we want to change the unit test WRT #11239 (comment) ?

@caitp caitp closed this in d02d058 Mar 6, 2015

caitp added a commit that referenced this pull request Mar 6, 2015

feat(ngMock): allow mock $controller service to set up controller bin…
…dings

Adds a new mock for the $controller service, in order to simplify testing using the
bindToController feature.

```js
var dictionaryOfControllerBindings = {
  data: [
    { id: 0, phone: '...', name: '...' },
    { id: 1, phone: '...', name: '...' },
  ]
};

// When the MyCtrl constructor is called, `this.data ~= dictionaryOfControllerBindings.data`
$controller(MyCtrl, myLocals, dictionaryOfControllerBindings);
```

Closes #9425
Closes #11239
@caitp

This comment has been minimized.

Show comment
Hide comment
@caitp

caitp Mar 6, 2015

Contributor

LGTM, merging with tweaked docs. Do we want to change the unit test WRT #11239 (comment) ?

sounds good to me

Contributor

caitp commented Mar 6, 2015

LGTM, merging with tweaked docs. Do we want to change the unit test WRT #11239 (comment) ?

sounds good to me

hansmaad pushed a commit to hansmaad/angular.js that referenced this pull request Mar 10, 2015

feat(ngMock): allow mock $controller service to set up controller bin…
…dings

Adds a new mock for the $controller service, in order to simplify testing using the
bindToController feature.

```js
var dictionaryOfControllerBindings = {
  data: [
    { id: 0, phone: '...', name: '...' },
    { id: 1, phone: '...', name: '...' },
  ]
};

// When the MyCtrl constructor is called, `this.data ~= dictionaryOfControllerBindings.data`
$controller(MyCtrl, myLocals, dictionaryOfControllerBindings);
```

Closes #9425
Closes #11239
@thelgevold

This comment has been minimized.

Show comment
Hide comment
@thelgevold

thelgevold Apr 4, 2015

This is great!
I have added a quick write up with an example of how to use it:
http://www.syntaxsuccess.com/viewarticle/5520197d61d7e9d80a9f52db
Thanks for adding this!

thelgevold commented Apr 4, 2015

This is great!
I have added a quick write up with an example of how to use it:
http://www.syntaxsuccess.com/viewarticle/5520197d61d7e9d80a9f52db
Thanks for adding this!

@petebacondarwin

This comment has been minimized.

Show comment
Hide comment
@petebacondarwin
Member

petebacondarwin commented Apr 5, 2015

Thanks @thelgevold

netman92 added a commit to netman92/angular.js that referenced this pull request Aug 8, 2015

feat(ngMock): allow mock $controller service to set up controller bin…
…dings

Adds a new mock for the $controller service, in order to simplify testing using the
bindToController feature.

```js
var dictionaryOfControllerBindings = {
  data: [
    { id: 0, phone: '...', name: '...' },
    { id: 1, phone: '...', name: '...' },
  ]
};

// When the MyCtrl constructor is called, `this.data ~= dictionaryOfControllerBindings.data`
$controller(MyCtrl, myLocals, dictionaryOfControllerBindings);
```

Closes #9425
Closes #11239

netman92 added a commit to netman92/angular.js that referenced this pull request Aug 8, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment