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

Closed
wants to merge 1 commit into
from

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

@caitp caitp 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
0dc9457
@gkalpak gkalpak commented on the diff Mar 5, 2015
src/ngMock/angular-mocks.js
+* controller constructor function. Otherwise it's considered to be a string which is used
+* to retrieve the controller constructor using the following steps:
+*
+* * check if a controller with given name is registered via `$controllerProvider`
+* * check if evaluating the string on the current scope returns a constructor
+* * if $controllerProvider#allowGlobals, check `window[constructor]` on the global
+* `window` object (not recommended)
+*
+* The string can use the `controller as property` syntax, where the controller instance is published
+* as the specified property on the `scope`; the `scope` must be injected into `locals` param for this
+* to work correctly.
+*
+* @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.
@gkalpak
gkalpak Mar 5, 2015 Member

oftests --> of tests

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

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

@cesarandreu

@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
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?

@petebacondarwin petebacondarwin commented on the diff Mar 6, 2015
test/ngMock/angular-mocksSpec.js
@@ -1751,6 +1751,28 @@ describe('ngMock', function() {
}));
});
});
+
+
+ describe('$controllerDecorator', function() {
+ it('should support creating controller with bindings', function() {
+ var called = false;
+ var data = [
+ { name: 'derp1', id: 0 },
+ { name: 'testname', id: 1 },
+ { name: 'flurp', id: 2 }
+ ];
+ module(function($controllerProvider) {
+ $controllerProvider.register('testCtrl', function() {
+ called = true;
+ expect(this.data).toEqual(data);
@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
Member

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

@caitp caitp added a commit that closed this pull request Mar 6, 2015
@caitp @petebacondarwin caitp + petebacondarwin 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
d02d058
@caitp caitp closed this in d02d058 Mar 6, 2015
@caitp caitp added a commit that referenced this pull request Mar 6, 2015
@caitp @petebacondarwin caitp + petebacondarwin 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
b3878a3
@caitp
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 hansmaad pushed a commit to hansmaad/angular.js that referenced this pull request Mar 10, 2015
@caitp caitp + hansmaad 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
f799ab7
@thelgevold

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
Member

Thanks @thelgevold

@netman92 netman92 added a commit to netman92/angular.js that referenced this pull request Aug 8, 2015
@caitp @netman92 caitp + netman92 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
048a9d8
@netman92 netman92 added a commit to netman92/angular.js that referenced this pull request Aug 8, 2015
@petebacondarwin @netman92 petebacondarwin + netman92 test(ngMock): test shallow copy of mock controller bindings
See #11239
3e98544
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment