From defc79f8004fa04b39d6c3c1a28bf69ffa7338b3 Mon Sep 17 00:00:00 2001 From: Andrew Archibald Date: Sat, 27 Jun 2015 11:47:09 -0700 Subject: [PATCH 1/9] Add bardjs definitions --- bardjs/bardjs.d.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 bardjs/bardjs.d.ts diff --git a/bardjs/bardjs.d.ts b/bardjs/bardjs.d.ts new file mode 100644 index 0000000000..77fc799b16 --- /dev/null +++ b/bardjs/bardjs.d.ts @@ -0,0 +1,30 @@ +// Type definitions for bardjs 0.1.4 +// Project: https://github.com/wardbell/bardjs +// Definitions by: Andrew Archibald +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// +/// +/// + +declare module bard { + function $httpBackend($provide: angular.auto.IProvideService); + function $q($provide: angular.auto.IProvideService); + function addGlobals(...args: any[]): void; + function appModule(...args: any[]); + function assertFail(message: string): Chai.AssertionError; + function asyncModule(...args: any[]); + function debugging(x); + function fakeLogger($provide: angular.auto.IProvideService): void; + function fakeRouteHelperProvider($provide: angular.auto.IProvideService): void; + function fakeRouteProvider($provide: angular.auto.IProvideService): void; + function fakeStateProvider($provide: angular.auto.IProvideService): void; + function fakeToastr($provide: angular.auto.IProvideService): void; + function inject(...args: any[]): void; + function log(msg); + function mochaRunnerListener(runner: Mocha.IRunner): void; + function mockService(service, config); + function replaceAccentChars(s: string): string; + function verifyNoOutstandingHttpRequests(): void; + function wrapWithDone(callback: Function, done: Function); +} From 79daa85ac468dc65ba208c775e5ef53780279b4f Mon Sep 17 00:00:00 2001 From: Andrew Archibald Date: Sat, 27 Jun 2015 21:13:51 -0700 Subject: [PATCH 2/9] Add more specific types and comments to bardjs --- bardjs/bardjs.d.ts | 166 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 155 insertions(+), 11 deletions(-) diff --git a/bardjs/bardjs.d.ts b/bardjs/bardjs.d.ts index 77fc799b16..328d5b067c 100644 --- a/bardjs/bardjs.d.ts +++ b/bardjs/bardjs.d.ts @@ -7,24 +7,168 @@ /// /// +/** + * Module for bardjs functions + */ declare module bard { - function $httpBackend($provide: angular.auto.IProvideService); - function $q($provide: angular.auto.IProvideService); - function addGlobals(...args: any[]): void; - function appModule(...args: any[]); + /** + * Replaces the ngMock'ed $httpBackend with the real one from ng thus + * restoring the ability to issue AJAX calls to the backend with $http. + * + * Note that $q remains ngMocked so you must flush $http calls ($rootScope.$digest). + * Use $rootScope.$apply() for this purpose. + * + * Could restore $q with $qReal in which case don't need to flush. + */ + function $httpBackend($provide: angular.auto.IProvideService): any; + + /** + * Replaces the ngMock'ed $q with the real one from ng thus + * obviating the need to flush $http and $q queues + * at the expense of ability to control $q timing. + */ + function $q($provide: angular.auto.IProvideService): any; + + /** + * Add names of globals to list of OK globals for this mocha spec + * NB: Call this method ONLY if you're using mocha! + * NB: Turn off browser-sync else mocha detects the browser-sync globals + * like ` ___browserSync___` + */ + function addGlobals(...globals: any[]): void; + + /** + * Prepare ngMocked application feature module + * along with faked toastr, routehelper, + * and faked router services. + * Especially useful for controller testing + * Use it as you would the ngMocks#module method + * + * DO NOT USE IF YOU NEED THE REAL ROUTER SERVICES! + * Fall back to `angular.mock.module(...)` or just `module(...)` + */ + function appModule(...args: any[]): any; + + /** + * Assert a failure in mocha, without condition + */ function assertFail(message: string): Chai.AssertionError; - function asyncModule(...args: any[]); - function debugging(x); + + /** + * Prepare ngMocked module definition that makes real $http and $q calls + * Also adds fakeLogger to the end of the definition + * Use it as you would the ngMocks#module method + */ + function asyncModule(...args: any[]): any; + + /** + * Get or set bard debugging flag + */ + function debugging(newFlag?: any): boolean; + + /** + * Creates a fake logger to be used in testing + */ function fakeLogger($provide: angular.auto.IProvideService): void; + + /** + * Creates a fake route helper provider to be used in testing + */ function fakeRouteHelperProvider($provide: angular.auto.IProvideService): void; + + /** + * Stub out the $routeProvider so we avoid + * all routing calls, including the default route + * which runs on every test otherwise. + * Make sure this goes before the inject in the spec. + */ function fakeRouteProvider($provide: angular.auto.IProvideService): void; + + /** + * Stub out the $stateProvider so we avoid + * all routing calls, including the default state + * which runs on every test otherwise. + * Make sure this goes before the inject in the spec. + */ function fakeStateProvider($provide: angular.auto.IProvideService): void; + + /** + * Creates a fake toastr to be used in testing + */ function fakeToastr($provide: angular.auto.IProvideService): void; - function inject(...args: any[]): void; - function log(msg); + + /** + * Inject selected services into the windows object during test + * then remove them when test ends with an `afterEach`. + * + * spares us the repetition of creating common service vars and injecting them + * + * Option: the first argument may be the mocha spec context object (`this`) + * It MUST be `this` if you what to check for mocha global leaks. + * Do NOT supply `this` as the first arg if you're not running mocha specs. + * + * remaining inject arguments may take one of 3 forms : + * + * function - This fn will be passed to ngMocks.inject. + * Annotations extracted after inject does its thing. + * [strings] - same string array you'd use to set fn.$inject + * (...string) - string arguments turned into a string array + */ + function inject(context?: any, ...args: string[]): void; + + /** + * Write to console if bard debugging flag is on + */ + function log(message: any): void; + + /** + * Listen to mocha test runner events + * Usage in browser: + * var runner = mocha.run(); + * bard.mochaRunnerListener(runner); + */ function mochaRunnerListener(runner: Mocha.IRunner): void; - function mockService(service, config); - function replaceAccentChars(s: string): string; + + /** + * Mocks out a service with sinon stubbed functions + * that return the values specified in the config + * + * If the config value is `undefined`, + * stub the service method with a dummy that doesn't return a value + * + * If the config value is a function, set service property with it + * + * If a service member is a property, not a function, + * set it with the config value + * If a service member name is not a key in config, + * follow the same logic as above to set its members + * using the config._default value (which is `undefined` if omitted) + * + * If there is a config entry that is NOT a member of the service + * add mocked function to the service using the config value + */ + function mockService(service: any, config: any): any; + + /** + * Replaces the accented characters of many European languages w/ unaccented chars + * Use it in JavaScript string sorts where such characters may be encountered + * Matches the default string comparers of most databases. + * Ex: replaceAccentChars(a.Name) < replaceAccentChars(b.Name) + * instead of: a.Name < b.Name + */ + function replaceAccentChars(text: string): string; + + /** + * Assert that there are no outstanding HTTP requests after test is complete + * For use with ngMocks; doesn't work for async server integration tests + */ function verifyNoOutstandingHttpRequests(): void; - function wrapWithDone(callback: Function, done: Function); + + /** + * Returns a function that execute a callback function + * (typically a fn making asserts) within a try/catch + * The try/catch then calls the ambient "done" function + * in the appropriate way for both success and failure + */ + function wrapWithDone(callback: Function, done: Function): Function; } From 9d20855e549fd43fcb71221f05e75be0e3e57418 Mon Sep 17 00:00:00 2001 From: Andrew Archibald Date: Sat, 27 Jun 2015 21:30:18 -0700 Subject: [PATCH 3/9] Made more specific comments in bardjs --- bardjs/bardjs.d.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bardjs/bardjs.d.ts b/bardjs/bardjs.d.ts index 328d5b067c..e0288825f7 100644 --- a/bardjs/bardjs.d.ts +++ b/bardjs/bardjs.d.ts @@ -67,12 +67,12 @@ declare module bard { function debugging(newFlag?: any): boolean; /** - * Creates a fake logger to be used in testing + * Registers a fake logger service that you can spy on */ function fakeLogger($provide: angular.auto.IProvideService): void; /** - * Creates a fake route helper provider to be used in testing + * Registers a fake route helper provider service that you can spy on */ function fakeRouteHelperProvider($provide: angular.auto.IProvideService): void; @@ -93,7 +93,7 @@ declare module bard { function fakeStateProvider($provide: angular.auto.IProvideService): void; /** - * Creates a fake toastr to be used in testing + * Registers a fake toastr service that you can spy on */ function fakeToastr($provide: angular.auto.IProvideService): void; From f91aabedc10bb94dd657d550f858af68077bf527 Mon Sep 17 00:00:00 2001 From: Andrew Archibald Date: Wed, 15 Jul 2015 17:08:47 -0700 Subject: [PATCH 4/9] Started bardjs tests; Made some bardjs types more clear. --- bardjs/bardjs-tests.ts | 158 +++++++++++++++++++++++++++++++++++++++++ bardjs/bardjs.d.ts | 6 +- 2 files changed, 161 insertions(+), 3 deletions(-) create mode 100644 bardjs/bardjs-tests.ts diff --git a/bardjs/bardjs-tests.ts b/bardjs/bardjs-tests.ts new file mode 100644 index 0000000000..9243c10080 --- /dev/null +++ b/bardjs/bardjs-tests.ts @@ -0,0 +1,158 @@ +/// +/// +/// +/// +/// + +var expect = chai.expect, assert = chai.assert; + +function someFunction() { } +var someObject = { }; +var myService; + +/* + * bard.$httpBackend + */ +var myService; + +beforeEach(module(bard.$httpBackend, 'app')); + +beforeEach(inject(function(_myService_) { + myService = _myService_; +})); + +it('should return valid data', function(done) { + myService.remoteCall() + .then(function(data) { + expect(data).to.exist; + }) + .then(done, done); + + $rootScope.$apply; // because not using bard.$q, must flush the $http and $q queues +}); + +/* + * bard.$q + */ +beforeEach(module(bard.$q, bard.$httpBackend, 'app')); + +beforeEach(inject(function(_myService_) { + myService = _myService_; +})); + +it('should return valid data', (done) => { + myService.remoteCall() + .then((data) => { + expect(data).to.exist; + }) + .then(done, done); + + // no need to flush +}); + +/* + * bard.addGlobals + */ + +/* + * bard.appModule + */ +beforeEach(bard.appModule('myModule')); + +beforeEach(bard.appModule('myModule', someFunction, someObject)); + +/* + * bard.assertFail + */ + +/* + * bard.asyncModule + */ +beforeEach(bard.asyncModule('app')); + +beforeEach(bard.asyncModule('myModule', someFunction, someObject)); + +/* + * bard.debugging + */ + +/* + * bard.fakeLogger + */ +beforeEach(module('myModule', bard.fakeLogger)); +beforeEach(bard.appModule('myModule', bard.fakeLogger)); +beforeEach(bard.asyncModule('myModule', bard.fakeLogger)); + +/* + * bard.fakeRouteHelperProvider + */ +beforeEach(module('myModule', bard.fakeRouteHelperProvider)); +beforeEach(bard.appModule('myModule', bard.fakeRouteHelperProvider)); +beforeEach(bard.asyncModule('myModule', bard.fakeRouteHelperProvider)); + +/* + * bard.fakeRouteProvider + */ +beforeEach(module('myModule', bard.fakeRouteProvider)); +beforeEach(bard.appModule('myModule', bard.fakeRouteProvider)); +beforeEach(bard.asyncModule('myModule', bard.fakeRouteProvider)); + +/* + * bard.fakeStateProvider + */ +beforeEach(module('myModule', bard.fakeStateProvider)); +beforeEach(bard.appModule('myModule', bard.fakeStateProvider)); +beforeEach(bard.asyncModule('myModule', bard.fakeStateProvider)); + +/* + * bard.fakeToastr + */ +beforeEach(module('myModule', bard.fakeToastr)); +beforeEach(bard.appModule('myModule', bard.fakeToastr)); +beforeEach(bard.asyncModule('myModule', bard.fakeToastr)); + +/* + * bard.inject + */ +beforeEach(() => bard.inject(this, '$controller', '$log', '$q', '$rootScope', 'dataservice')); + +/* + * bard.log + */ +bard.log('We got the goods'); + +/* + * bard.mochaRunnerListener + */ + +/* + * bard.mockService + */ +var controller; +var avengers = [{name: 'Captain America' /* ... */}]; +var $controller, $q, $rootScope, dataservice; + +beforeEach(function() { + bard.appModule('app.avengers'); + bard.inject(this, '$controller', '$q', '$rootScope', 'dataservice'); + + bard.mockService(dataservice, { + getAvengers: $q.when(avengers), + _default: $q.when([]) + }); + + controller = $controller('Avengers'); + $rootScope.$apply(); +}); + +/* + * bard.replaceAccentChars + */ + +/* + * bard.verifyNoOutstandingHttpRequests + */ + +/* + * bard.wrapWithDone + */ diff --git a/bardjs/bardjs.d.ts b/bardjs/bardjs.d.ts index e0288825f7..3d3ad5f228 100644 --- a/bardjs/bardjs.d.ts +++ b/bardjs/bardjs.d.ts @@ -47,7 +47,7 @@ declare module bard { * DO NOT USE IF YOU NEED THE REAL ROUTER SERVICES! * Fall back to `angular.mock.module(...)` or just `module(...)` */ - function appModule(...args: any[]): any; + function appModule(...fns: (string | Function | Object)[]): () => void; /** * Assert a failure in mocha, without condition @@ -59,7 +59,7 @@ declare module bard { * Also adds fakeLogger to the end of the definition * Use it as you would the ngMocks#module method */ - function asyncModule(...args: any[]): any; + function asyncModule(...fns: (string | Function | Object)[]): () => void; /** * Get or set bard debugging flag @@ -114,7 +114,7 @@ declare module bard { * [strings] - same string array you'd use to set fn.$inject * (...string) - string arguments turned into a string array */ - function inject(context?: any, ...args: string[]): void; + function inject(context?: Function, ...args: string[]): void; /** * Write to console if bard debugging flag is on From e28aef1c025ec1f6377cc1a4cbba5ac256e8840a Mon Sep 17 00:00:00 2001 From: Andrew Archibald Date: Wed, 15 Jul 2015 21:35:55 -0700 Subject: [PATCH 5/9] Fixed some issues with bard tests --- bardjs/bardjs-tests.ts | 372 +++++++++++++++++++++++++---------------- 1 file changed, 226 insertions(+), 146 deletions(-) diff --git a/bardjs/bardjs-tests.ts b/bardjs/bardjs-tests.ts index 9243c10080..d15cc3d182 100644 --- a/bardjs/bardjs-tests.ts +++ b/bardjs/bardjs-tests.ts @@ -4,155 +4,235 @@ /// /// -var expect = chai.expect, assert = chai.assert; +var expect = chai.expect, + assert = chai.assert; -function someFunction() { } -var someObject = { }; -var myService; - -/* - * bard.$httpBackend - */ -var myService; - -beforeEach(module(bard.$httpBackend, 'app')); - -beforeEach(inject(function(_myService_) { - myService = _myService_; -})); - -it('should return valid data', function(done) { - myService.remoteCall() - .then(function(data) { - expect(data).to.exist; - }) - .then(done, done); - - $rootScope.$apply; // because not using bard.$q, must flush the $http and $q queues -}); - -/* - * bard.$q - */ -beforeEach(module(bard.$q, bard.$httpBackend, 'app')); - -beforeEach(inject(function(_myService_) { - myService = _myService_; -})); - -it('should return valid data', (done) => { - myService.remoteCall() - .then((data) => { - expect(data).to.exist; - }) - .then(done, done); - - // no need to flush -}); - -/* - * bard.addGlobals - */ - -/* - * bard.appModule - */ -beforeEach(bard.appModule('myModule')); - -beforeEach(bard.appModule('myModule', someFunction, someObject)); - -/* - * bard.assertFail - */ - -/* - * bard.asyncModule - */ -beforeEach(bard.asyncModule('app')); - -beforeEach(bard.asyncModule('myModule', someFunction, someObject)); - -/* - * bard.debugging - */ - -/* - * bard.fakeLogger - */ -beforeEach(module('myModule', bard.fakeLogger)); -beforeEach(bard.appModule('myModule', bard.fakeLogger)); -beforeEach(bard.asyncModule('myModule', bard.fakeLogger)); - -/* - * bard.fakeRouteHelperProvider - */ -beforeEach(module('myModule', bard.fakeRouteHelperProvider)); -beforeEach(bard.appModule('myModule', bard.fakeRouteHelperProvider)); -beforeEach(bard.asyncModule('myModule', bard.fakeRouteHelperProvider)); - -/* - * bard.fakeRouteProvider - */ -beforeEach(module('myModule', bard.fakeRouteProvider)); -beforeEach(bard.appModule('myModule', bard.fakeRouteProvider)); -beforeEach(bard.asyncModule('myModule', bard.fakeRouteProvider)); - -/* - * bard.fakeStateProvider - */ -beforeEach(module('myModule', bard.fakeStateProvider)); -beforeEach(bard.appModule('myModule', bard.fakeStateProvider)); -beforeEach(bard.asyncModule('myModule', bard.fakeStateProvider)); - -/* - * bard.fakeToastr - */ -beforeEach(module('myModule', bard.fakeToastr)); -beforeEach(bard.appModule('myModule', bard.fakeToastr)); -beforeEach(bard.asyncModule('myModule', bard.fakeToastr)); - -/* - * bard.inject - */ -beforeEach(() => bard.inject(this, '$controller', '$log', '$q', '$rootScope', 'dataservice')); - -/* - * bard.log - */ -bard.log('We got the goods'); - -/* - * bard.mochaRunnerListener - */ - -/* - * bard.mockService - */ -var controller; -var avengers = [{name: 'Captain America' /* ... */}]; -var $controller, $q, $rootScope, dataservice; - -beforeEach(function() { - bard.appModule('app.avengers'); - bard.inject(this, '$controller', '$q', '$rootScope', 'dataservice'); - - bard.mockService(dataservice, { - getAvengers: $q.when(avengers), - _default: $q.when([]) +class MyService { + static $inject = ['$q']; + + constructor(private $q: angular.IQService) {} + + remoteCall(): angular.IPromise { + return new this.$q((resolve, reject) => { + resolve('Here is some data'); }); + } +} - controller = $controller('Avengers'); - $rootScope.$apply(); -}); +function myService($q: angular.IQService) { + return new MyService($q); +} -/* - * bard.replaceAccentChars - */ +angular + .module('tests') + .service('myService', myService); -/* - * bard.verifyNoOutstandingHttpRequests - */ -/* - * bard.wrapWithDone - */ + +module bardTests { + /* + * bard.$httpBackend + */ + function test_$httpBackend() { + var myService: MyService; + var $rootScope: any; + + beforeEach(module(bard.$httpBackend, 'app')); + + beforeEach(inject(function(_myService_: MyService, _$rootScope_: any) { + myService = _myService_; + $rootScope = _$rootScope_; + })); + + it('should return valid data', function(done) { + myService.remoteCall() + .then(function(data) { + expect(data).to.exist; + }) + .then(done, done); + + $rootScope.$apply; // because not using bard.$q, must flush the $http and $q queues + }); + } + + /* + * bard.$q + */ + function test_$q() { + var myService: MyService; + + beforeEach(module(bard.$q, bard.$httpBackend, 'app')); + + beforeEach(inject(function(_myService_) { + myService = _myService_; + })); + + it('should return valid data', (done) => { + myService.remoteCall() + .then((data) => { + expect(data).to.exist; + }) + .then(done, done); + + // no need to flush + }); + } + + /* + * bard.addGlobals + */ + function test_addGlobals() { + + } + + /* + * bard.appModule + */ + function test_appModule() { + beforeEach(bard.appModule('myModule')); + //// + beforeEach(bard.appModule('myModule', function() {}, {})); + } + + /* + * bard.assertFail + */ + function test_assertFail() { + + } + + /* + * bard.asyncModule + */ + function test_asyncModule() { + beforeEach(bard.asyncModule('app')); + //// + beforeEach(bard.asyncModule('myModule', function() {}, {})); + } + + /* + * bard.debugging + */ + function test_debugging() { + + } + + /* + * bard.fakeLogger + */ + function test_fakeLogger() { + beforeEach(module('myModule', bard.fakeLogger)); + //// + beforeEach(bard.appModule('myModule', bard.fakeLogger)); + //// + beforeEach(bard.asyncModule('myModule', bard.fakeLogger)); + } + + /* + * bard.fakeRouteHelperProvider + */ + function test_fakeRouteHelperProvider() { + beforeEach(module('myModule', bard.fakeRouteHelperProvider)); + //// + beforeEach(bard.appModule('myModule', bard.fakeRouteHelperProvider)); + //// + beforeEach(bard.asyncModule('myModule', bard.fakeRouteHelperProvider)); + } + + /* + * bard.fakeRouteProvider + */ + function test_fakeRouteProvider() { + beforeEach(module('myModule', bard.fakeRouteProvider)); + //// + beforeEach(bard.appModule('myModule', bard.fakeRouteProvider)); + //// + beforeEach(bard.asyncModule('myModule', bard.fakeRouteProvider)); + } + + /* + * bard.fakeStateProvider + */ + function test_fakeStateProvider() { + beforeEach(module('myModule', bard.fakeStateProvider)); + //// + beforeEach(bard.appModule('myModule', bard.fakeStateProvider)); + //// + beforeEach(bard.asyncModule('myModule', bard.fakeStateProvider)); + } + + /* + * bard.fakeToastr + */ + function test_fakeToastr() { + beforeEach(module('myModule', bard.fakeToastr)); + //// + beforeEach(bard.appModule('myModule', bard.fakeToastr)); + //// + beforeEach(bard.asyncModule('myModule', bard.fakeToastr)); + } + + /* + * bard.inject + */ + function test_inject() { + beforeEach(() => bard.inject(this, '$controller', '$log', '$q', '$rootScope', 'dataservice')); + } + + /* + * bard.log + */ + function test_log() { + bard.log('We got the goods'); + } + + /* + * bard.mochaRunnerListener + */ + function test_mochaRunnerListener() { + + } + + /* + * bard.mockService + */ + function test_mockService() { + var controller; + var avengers = [{name: 'Captain America' /* ... */}]; + var $controller, $q, $rootScope, dataservice; + + beforeEach(function() { + bard.appModule('app.avengers'); + bard.inject(this, '$controller', '$q', '$rootScope', 'dataservice'); + + bard.mockService(dataservice, { + getAvengers: $q.when(avengers), + _default: $q.when([]) + }); + + controller = $controller('Avengers'); + $rootScope.$apply(); + }); + } + + /* + * bard.replaceAccentChars + */ + function test_replaceAccentChars() { + + } + + /* + * bard.verifyNoOutstandingHttpRequests + */ + function test_verifyNoOutstandingHttpRequests() { + + } + + /* + * bard.wrapWithDone + */ + function test_wrapWithDone() { + + } +} \ No newline at end of file From e80350d66819f909e50bbccef3e74f6205fb95a6 Mon Sep 17 00:00:00 2001 From: Andrew Archibald Date: Wed, 15 Jul 2015 21:51:59 -0700 Subject: [PATCH 6/9] Fixed some bardjs implicit 'any' types --- bardjs/bardjs-tests.ts | 67 +++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/bardjs/bardjs-tests.ts b/bardjs/bardjs-tests.ts index d15cc3d182..8083fd3f2c 100644 --- a/bardjs/bardjs-tests.ts +++ b/bardjs/bardjs-tests.ts @@ -4,42 +4,40 @@ /// /// -var expect = chai.expect, - assert = chai.assert; - -class MyService { - static $inject = ['$q']; - - constructor(private $q: angular.IQService) {} - - remoteCall(): angular.IPromise { - return new this.$q((resolve, reject) => { - resolve('Here is some data'); - }); - } -} - -function myService($q: angular.IQService) { - return new MyService($q); -} - -angular - .module('tests') - .service('myService', myService); - - - module bardTests { + var expect = chai.expect, + assert = chai.assert; + + class MyService { + static $inject = ['$q']; + + constructor(private $q: angular.IQService) {} + + remoteCall(): angular.IPromise { + return new this.$q((resolve, reject) => { + resolve(['Hello', 'World']); + }); + } + } + + function myService($q: angular.IQService) { + return new MyService($q); + } + + angular + .module('bardTests') + .service('myService', myService); + /* * bard.$httpBackend */ function test_$httpBackend() { var myService: MyService; - var $rootScope: any; + var $rootScope: angular.IRootScopeService; beforeEach(module(bard.$httpBackend, 'app')); - beforeEach(inject(function(_myService_: MyService, _$rootScope_: any) { + beforeEach(inject(function(_myService_: MyService, _$rootScope_: angular.IRootScopeService) { myService = _myService_; $rootScope = _$rootScope_; })); @@ -51,7 +49,7 @@ module bardTests { }) .then(done, done); - $rootScope.$apply; // because not using bard.$q, must flush the $http and $q queues + $rootScope.$apply; // Because not using bard.$q, must flush the $http and $q queues }); } @@ -63,7 +61,7 @@ module bardTests { beforeEach(module(bard.$q, bard.$httpBackend, 'app')); - beforeEach(inject(function(_myService_) { + beforeEach(inject(function(_myService_: MyService) { myService = _myService_; })); @@ -74,7 +72,7 @@ module bardTests { }) .then(done, done); - // no need to flush + // No need to flush }); } @@ -176,7 +174,7 @@ module bardTests { * bard.inject */ function test_inject() { - beforeEach(() => bard.inject(this, '$controller', '$log', '$q', '$rootScope', 'dataservice')); + beforeEach(() => bard.inject(this, '$controller', '$log', '$q', '$rootScope', 'myService')); } /* @@ -199,11 +197,14 @@ module bardTests { function test_mockService() { var controller; var avengers = [{name: 'Captain America' /* ... */}]; - var $controller, $q, $rootScope, dataservice; + var $controller: angular.IControllerService, + $q: angular.IQService, + $rootScope: angular.IRootScopeService, + myService: MyService; beforeEach(function() { bard.appModule('app.avengers'); - bard.inject(this, '$controller', '$q', '$rootScope', 'dataservice'); + bard.inject(this, '$controller', '$q', '$rootScope', 'myService'); bard.mockService(dataservice, { getAvengers: $q.when(avengers), From 6de3f1f79c3470d02f41f9319839879c74b1328b Mon Sep 17 00:00:00 2001 From: Andrew Archibald Date: Wed, 15 Jul 2015 22:19:10 -0700 Subject: [PATCH 7/9] Added more tests to bardjs-tests.ts --- bardjs/bardjs-tests.ts | 462 ++++++++++++++++++++++------------------- 1 file changed, 253 insertions(+), 209 deletions(-) diff --git a/bardjs/bardjs-tests.ts b/bardjs/bardjs-tests.ts index 8083fd3f2c..3eedd970b3 100644 --- a/bardjs/bardjs-tests.ts +++ b/bardjs/bardjs-tests.ts @@ -5,235 +5,279 @@ /// module bardTests { - var expect = chai.expect, - assert = chai.assert; + var expect = chai.expect, + assert = chai.assert; - class MyService { - static $inject = ['$q']; + class MyService { + static $inject = ['$q']; - constructor(private $q: angular.IQService) {} + constructor(private $q: angular.IQService) {} - remoteCall(): angular.IPromise { - return new this.$q((resolve, reject) => { - resolve(['Hello', 'World']); - }); + remoteCall(): angular.IPromise { + return new this.$q((resolve, reject) => { + resolve(['Hello', 'World']); + }); + } } - } - function myService($q: angular.IQService) { - return new MyService($q); - } + function myService($q: angular.IQService) { + return new MyService($q); + } - angular - .module('bardTests') - .service('myService', myService); + class MyController { + myProperty: string; + } - /* - * bard.$httpBackend - */ - function test_$httpBackend() { - var myService: MyService; - var $rootScope: angular.IRootScopeService; + angular + .module('myModule') + .service('myService', myService) + .controller('MyController', MyController); - beforeEach(module(bard.$httpBackend, 'app')); + /* + * bard.$httpBackend + */ + function test_$httpBackend() { + var myService: MyService; + var $rootScope: angular.IRootScopeService; - beforeEach(inject(function(_myService_: MyService, _$rootScope_: angular.IRootScopeService) { - myService = _myService_; - $rootScope = _$rootScope_; - })); + beforeEach(module(bard.$httpBackend, 'myModule')); - it('should return valid data', function(done) { - myService.remoteCall() - .then(function(data) { - expect(data).to.exist; + beforeEach(inject(function(_myService_: MyService, _$rootScope_: angular.IRootScopeService) { + myService = _myService_; + $rootScope = _$rootScope_; + })); + + it('should return valid data', function(done) { + myService.remoteCall() + .then(function(data) { + expect(data).to.exist; + }) + .then(done, done); + + $rootScope.$apply; // Because not using bard.$q, must flush the $http and $q queues + }); + } + + /* + * bard.$q + */ + function test_$q() { + var myService: MyService; + + beforeEach(module(bard.$q, bard.$httpBackend, 'myModule')); + + beforeEach(inject(function(_myService_: MyService) { + myService = _myService_; + })); + + it('should return valid data', (done) => { + myService.remoteCall() + .then((data) => { + expect(data).to.exist; + }) + .then(done, done); + + // No need to flush + }); + } + + /* + * bard.addGlobals + */ + function test_addGlobals() { + describe('someting', function() { + var ctx = this; + + it('should work', function() { + var bar = 'bar'; + bard.addGlobals(this, 'foo'); // where `this` is the spec context + bard.addGlobals(this, 'foo', bar); + bard.addGlobals.bind(this)('foo', 'bar'); + bard.addGlobals(ctx, ['foo', 'bar']) // where ctx is the spec context + }); }) - .then(done, done); + } - $rootScope.$apply; // Because not using bard.$q, must flush the $http and $q queues - }); - } - - /* - * bard.$q - */ - function test_$q() { - var myService: MyService; + /* + * bard.appModule + */ + function test_appModule() { + beforeEach(bard.appModule('myModule')); + //// + beforeEach(bard.appModule('myModule', function() {}, {})); + } - beforeEach(module(bard.$q, bard.$httpBackend, 'app')); + /* + * bard.assertFail + */ + function test_assertFail() { + bard.assertFail('FAIL!'); + } - beforeEach(inject(function(_myService_: MyService) { - myService = _myService_; - })); + /* + * bard.asyncModule + */ + function test_asyncModule() { + beforeEach(bard.asyncModule('myModule')); + //// + beforeEach(bard.asyncModule('myModule', function() {}, {})); + } - it('should return valid data', (done) => { - myService.remoteCall() - .then((data) => { - expect(data).to.exist; - }) - .then(done, done); + /* + * bard.debugging + */ + function test_debugging() { + console.log( + bard.debugging(true), // should be true + bard.debugging(false), // should be false + bard.debugging(42), // should be true + bard.debugging(''), // should be false + bard.debugging() // should be false + ); + } - // No need to flush - }); - } - - /* - * bard.addGlobals - */ - function test_addGlobals() { - - } + /* + * bard.fakeLogger + */ + function test_fakeLogger() { + beforeEach(module('myModule', bard.fakeLogger)); + //// + beforeEach(bard.appModule('myModule', bard.fakeLogger)); + //// + beforeEach(bard.asyncModule('myModule', bard.fakeLogger)); + } - /* - * bard.appModule - */ - function test_appModule() { - beforeEach(bard.appModule('myModule')); - //// - beforeEach(bard.appModule('myModule', function() {}, {})); - } + /* + * bard.fakeRouteHelperProvider + */ + function test_fakeRouteHelperProvider() { + beforeEach(module('myModule', bard.fakeRouteHelperProvider)); + //// + beforeEach(bard.appModule('myModule', bard.fakeRouteHelperProvider)); + //// + beforeEach(bard.asyncModule('myModule', bard.fakeRouteHelperProvider)); + } - /* - * bard.assertFail - */ - function test_assertFail() { - - } + /* + * bard.fakeRouteProvider + */ + function test_fakeRouteProvider() { + beforeEach(module('myModule', bard.fakeRouteProvider)); + //// + beforeEach(bard.appModule('myModule', bard.fakeRouteProvider)); + //// + beforeEach(bard.asyncModule('myModule', bard.fakeRouteProvider)); + } - /* - * bard.asyncModule - */ - function test_asyncModule() { - beforeEach(bard.asyncModule('app')); - //// - beforeEach(bard.asyncModule('myModule', function() {}, {})); - } - - /* - * bard.debugging - */ - function test_debugging() { - - } - - /* - * bard.fakeLogger - */ - function test_fakeLogger() { - beforeEach(module('myModule', bard.fakeLogger)); - //// - beforeEach(bard.appModule('myModule', bard.fakeLogger)); - //// - beforeEach(bard.asyncModule('myModule', bard.fakeLogger)); - } - - /* - * bard.fakeRouteHelperProvider - */ - function test_fakeRouteHelperProvider() { - beforeEach(module('myModule', bard.fakeRouteHelperProvider)); - //// - beforeEach(bard.appModule('myModule', bard.fakeRouteHelperProvider)); - //// - beforeEach(bard.asyncModule('myModule', bard.fakeRouteHelperProvider)); - } - - /* - * bard.fakeRouteProvider - */ - function test_fakeRouteProvider() { - beforeEach(module('myModule', bard.fakeRouteProvider)); - //// - beforeEach(bard.appModule('myModule', bard.fakeRouteProvider)); - //// - beforeEach(bard.asyncModule('myModule', bard.fakeRouteProvider)); - } - - /* - * bard.fakeStateProvider - */ - function test_fakeStateProvider() { - beforeEach(module('myModule', bard.fakeStateProvider)); - //// - beforeEach(bard.appModule('myModule', bard.fakeStateProvider)); - //// - beforeEach(bard.asyncModule('myModule', bard.fakeStateProvider)); - } - - /* - * bard.fakeToastr - */ - function test_fakeToastr() { - beforeEach(module('myModule', bard.fakeToastr)); - //// - beforeEach(bard.appModule('myModule', bard.fakeToastr)); - //// - beforeEach(bard.asyncModule('myModule', bard.fakeToastr)); - } - - /* - * bard.inject - */ - function test_inject() { - beforeEach(() => bard.inject(this, '$controller', '$log', '$q', '$rootScope', 'myService')); - } - - /* - * bard.log - */ - function test_log() { - bard.log('We got the goods'); - } - - /* - * bard.mochaRunnerListener - */ - function test_mochaRunnerListener() { - - } - - /* - * bard.mockService - */ - function test_mockService() { - var controller; - var avengers = [{name: 'Captain America' /* ... */}]; - var $controller: angular.IControllerService, - $q: angular.IQService, - $rootScope: angular.IRootScopeService, - myService: MyService; + /* + * bard.fakeStateProvider + */ + function test_fakeStateProvider() { + beforeEach(module('myModule', bard.fakeStateProvider)); + //// + beforeEach(bard.appModule('myModule', bard.fakeStateProvider)); + //// + beforeEach(bard.asyncModule('myModule', bard.fakeStateProvider)); + } - beforeEach(function() { - bard.appModule('app.avengers'); - bard.inject(this, '$controller', '$q', '$rootScope', 'myService'); + /* + * bard.fakeToastr + */ + function test_fakeToastr() { + beforeEach(module('myModule', bard.fakeToastr)); + //// + beforeEach(bard.appModule('myModule', bard.fakeToastr)); + //// + beforeEach(bard.asyncModule('myModule', bard.fakeToastr)); + } - bard.mockService(dataservice, { - getAvengers: $q.when(avengers), - _default: $q.when([]) - }); + /* + * bard.inject + */ + function test_inject() { + beforeEach(() => bard.inject(this, '$controller', '$log', '$q', '$rootScope', 'myService')); + } - controller = $controller('Avengers'); - $rootScope.$apply(); - }); - } - - /* - * bard.replaceAccentChars - */ - function test_replaceAccentChars() { - - } - - /* - * bard.verifyNoOutstandingHttpRequests - */ - function test_verifyNoOutstandingHttpRequests() { - - } - - /* - * bard.wrapWithDone - */ - function test_wrapWithDone() { - - } + /* + * bard.log + */ + function test_log() { + bard.log('We got the goods'); + } + + /* + * bard.mochaRunnerListener + */ + function test_mochaRunnerListener() { + var runner = mocha.run(); + bard.mochaRunnerListener(runner); + } + + /* + * bard.mockService + */ + function test_mockService() { + var controller: MyController, + myArray = ['This', 'is', 'some', 'mocked', 'data']; + var $controller: angular.IControllerService, + $q: angular.IQService, + $rootScope: angular.IRootScopeService, + myService: MyService; + + beforeEach(function() { + bard.appModule('myModule'); + bard.inject(this, '$controller', '$q', '$rootScope', 'myService'); + + bard.mockService(myService, { + remoteCall: $q.when(myArray), + _default: $q.when([]) + }); + + controller = $controller('MyController'); + $rootScope.$apply(); + }); + } + + /* + * bard.replaceAccentChars + */ + function test_replaceAccentChars() { + console.log(bard.replaceAccentChars('àáâãäåèéêëìíîïòóôõöùúûüýÿ') === 'aaaaaaeeeeeeeeooooouuuuyy'); + } + + /* + * bard.verifyNoOutstandingHttpRequests + */ + function test_verifyNoOutstandingHttpRequests() { + var controller: MyController, + myArray = ['This', 'is', 'some', 'mocked', 'data']; + var $controller: angular.IControllerService, + $q: angular.IQService, + $rootScope: angular.IRootScopeService, + myService: MyService; + + beforeEach(function() { + bard.appModule('myModule'); + bard.inject(this, '$controller', '$q', '$rootScope', 'myService'); + + bard.mockService(myService, { + remoteCall: $q.when(myArray), + _default: $q.when([]) + }); + + controller = $controller('MyController'); + $rootScope.$apply(); + }); + + bard.verifyNoOutstandingHttpRequests(); + } + + /* + * bard.wrapWithDone + */ + function test_wrapWithDone() { + function callback() { console.log('Doing something...'); } + function done() { console.log('...Done'); } + bard.wrapWithDone(callback, done); + } } \ No newline at end of file From e6b1d5f73b03df88acabef15a393e4a52e19e681 Mon Sep 17 00:00:00 2001 From: Andrew Archibald Date: Thu, 16 Jul 2015 09:49:34 -0700 Subject: [PATCH 8/9] Formatted bardjs tests. --- bardjs/bardjs-tests.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bardjs/bardjs-tests.ts b/bardjs/bardjs-tests.ts index 3eedd970b3..3aba6cade2 100644 --- a/bardjs/bardjs-tests.ts +++ b/bardjs/bardjs-tests.ts @@ -128,11 +128,11 @@ module bardTests { */ function test_debugging() { console.log( - bard.debugging(true), // should be true - bard.debugging(false), // should be false - bard.debugging(42), // should be true - bard.debugging(''), // should be false - bard.debugging() // should be false + bard.debugging(true), // should return true + bard.debugging(false), // should return false + bard.debugging(42), // should return true + bard.debugging(''), // should return false + bard.debugging() // should return false ); } @@ -280,4 +280,4 @@ module bardTests { function done() { console.log('...Done'); } bard.wrapWithDone(callback, done); } -} \ No newline at end of file +} From 7fcdbd1a9b5ebdf39b5ac8e51ce72adfde685d74 Mon Sep 17 00:00:00 2001 From: Andrew Archibald Date: Thu, 16 Jul 2015 10:14:38 -0700 Subject: [PATCH 9/9] Made myService in bard tests minification safe --- bardjs/bardjs-tests.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bardjs/bardjs-tests.ts b/bardjs/bardjs-tests.ts index 3aba6cade2..b033ffe3c7 100644 --- a/bardjs/bardjs-tests.ts +++ b/bardjs/bardjs-tests.ts @@ -9,8 +9,6 @@ module bardTests { assert = chai.assert; class MyService { - static $inject = ['$q']; - constructor(private $q: angular.IQService) {} remoteCall(): angular.IPromise { @@ -20,6 +18,7 @@ module bardTests { } } + myService.$inject = ['$q']; function myService($q: angular.IQService) { return new MyService($q); }