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

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

Closed
wants to merge 1 commit into from

Conversation

@caitp
Copy link
Contributor

@caitp 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
Copy link
Contributor Author

@caitp 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.

…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
@caitp caitp force-pushed the caitp:issue-9425 branch from 94606d8 to 0dc9457 Mar 4, 2015
* @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

@caitp
Copy link
Contributor Author

@caitp 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
Copy link

@SonofNun15 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
Copy link

@cesarandreu 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
Copy link
Contributor Author

@caitp 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
Copy link
Member

@petebacondarwin 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
…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
Copy link
Contributor Author

@caitp 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
…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 angular#9425
Closes angular#11239
petebacondarwin added a commit that referenced this pull request Mar 12, 2015
petebacondarwin added a commit that referenced this pull request Mar 12, 2015
@thelgevold
Copy link

@thelgevold 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
Copy link
Member

@petebacondarwin petebacondarwin commented Apr 5, 2015

Thanks @thelgevold

netman92 added a commit to netman92/angular.js that referenced this pull request Aug 8, 2015
…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 angular#9425
Closes angular#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
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

7 participants