tennisgent / quickmock

quickmock is an simple service for automatically injecting mocks into your AngularJS unit tests using Jasmine or Mocha
http://tennisgent.github.io/quickmock
34 stars 14 forks source link

$scope is not available on a directive until after first call to $compile() #8

Open tennisgent opened 9 years ago

tennisgent commented 9 years ago

.$scope needs to be added to the directive object when quickmock() is initially called, rather than during the .$compile() phase, so that the scope properties can be modified before the compile happens.

Right now this requires a dummy call to .$compile() so that the .$scope first shows up:

var directive = quickmock({
    providerName: 'myDirective',
    moduleName: 'myModule',
    html: '<div my-directive></div>'
});
// directive.$scope is undefined here
directive.$compile();
// directive.$scope is now defined
directive.$scope.key = "value";
// can now use `key` property on scope
directive.$compile('<div my-directive some-property="key"></div>');

What we need is the ability to do this:

var directive = quickmock({
    providerName: 'myDirective',
    moduleName: 'myModule',
    html: '<div my-directive some-property="key"></div>'
});
directive.$scope.key = "value";
directive.$compile();  // this will now use the scope's `key` value during compile process
javascriptjedi commented 9 years ago

Yeah, that looks good. Or something like:

var directive = quickmock({
    providerName: 'myDirective',
    moduleName: 'myModule',
    scope: POJOWithPropertiesThatAreMappedOntoScope,
    html: '<div my-directive some-property="key"></div>'
});
directive.$compile();
tennisgent commented 9 years ago

Oh okay. I see. Just do like a angular.extend(newScope, configScope) to merge all the properties from the config scope value onto the scope that is created by quickmock. I think I'll add both ways of doing it :)