/tests/controllers/signup_controller_spec.js

https://github.com/blockchain/My-Wallet-V3-Frontend · JavaScript · 199 lines · 164 code · 35 blank · 0 comment · 4 complexity · 451abf3149e35370bce30975b6bf1dbb MD5 · raw file

  1. describe('SignupCtrl', () => {
  2. let scope;
  3. let modalInstance = {
  4. close () {},
  5. dismiss () {}
  6. };
  7. let $httpBackend;
  8. beforeEach(angular.mock.module('walletDirectives'));
  9. beforeEach(angular.mock.module('walletApp'));
  10. beforeEach(() => {
  11. module(($provide) => {
  12. $provide.factory('Env', ($q) => $q.resolve({
  13. rootURL: 'https://blockchain.info/',
  14. webHardFork: {
  15. balanceMessage: { 'en': 'Balance message' }
  16. }
  17. }));
  18. });
  19. });
  20. beforeEach(function () {
  21. angular.mock.inject(function ($injector, $rootScope, $controller, $compile, $templateCache) {
  22. $httpBackend = $injector.get('$httpBackend');
  23. $httpBackend.expectGET('https://blockchain.info/wallet/browser-info').respond({country_code: 'NL'});
  24. let Wallet = $injector.get('Wallet');
  25. Wallet.my.browserCheck = () => true;
  26. Wallet.my.browserCheckFast = () => true;
  27. let $state = $injector.get('$state'); // This is a mock
  28. $state.params = {email: ''};
  29. Wallet.login = (uid, pass, code, twoFactor, success, error) => success();
  30. Wallet.create = (password, email, currency, language, success) => success('new_guid');
  31. Wallet.settings_api = {
  32. changeLanguage (code, success) { return success(); },
  33. changeLocalCurrency () {}
  34. };
  35. Wallet.changeCurrency = function () {};
  36. scope = $rootScope.$new();
  37. let template = $templateCache.get('partials/signup.pug');
  38. $controller('SignupCtrl', {
  39. $scope: scope,
  40. $stateParams: {},
  41. $uibModalInstance: modalInstance
  42. }
  43. );
  44. scope.model = { fields: { email: '', password: '', confirmation: '', acceptedAgreement: false } };
  45. $compile(template)(scope);
  46. scope.$digest();
  47. });
  48. });
  49. it('should have initial values', () => {
  50. expect(scope.fields.email).toBeDefined();
  51. expect(scope.fields.password).toBeDefined();
  52. expect(scope.fields.confirmation).toBeDefined();
  53. expect(scope.fields.acceptedAgreement).toBe(false);
  54. });
  55. it('should not register when invalid', () => {
  56. spyOn(scope, 'createWallet');
  57. scope.signupForm.password.$setViewValue('');
  58. scope.$digest();
  59. scope.signup();
  60. scope.$digest();
  61. expect(scope.createWallet).not.toHaveBeenCalled();
  62. });
  63. describe('password', () => {
  64. beforeEach(function () {
  65. let form = scope.signupForm;
  66. form.email.$setViewValue('a@b.com');
  67. form.agreement.$setViewValue(true);
  68. return scope.$digest();
  69. });
  70. it('should not have an error if password confirmation matches', () => {
  71. scope.signupForm.password.$setViewValue('testing');
  72. scope.signupForm.confirmation.$setViewValue('testing');
  73. scope.$digest();
  74. expect(scope.signupForm.confirmation.$valid).toBe(true);
  75. });
  76. it('should have an error if password confirmation does not match', () => {
  77. scope.signupForm.password.$setViewValue('testing');
  78. scope.signupForm.confirmation.$setViewValue('wrong');
  79. scope.$digest();
  80. expect(scope.signupForm.confirmation.$valid).toBe(false);
  81. });
  82. });
  83. describe('agreement', () => {
  84. beforeEach(function () {
  85. let form = scope.signupForm;
  86. form.email.$setViewValue('a@b.com');
  87. form.password.$setViewValue('my_password12345');
  88. form.confirmation.$setViewValue('my_password12345');
  89. return scope.$digest();
  90. });
  91. it('should not be signed by default', () => expect(scope.fields.acceptedAgreement).toBe(false));
  92. it('should be signed by the user to register', () => {
  93. expect(scope.signupForm.$valid).toBe(false);
  94. scope.signupForm.agreement.$setViewValue(true);
  95. scope.$digest();
  96. expect(scope.signupForm.$valid).toBe(true);
  97. });
  98. });
  99. describe('signup()', () => {
  100. beforeEach(function () {
  101. let form = scope.signupForm;
  102. form.email.$setViewValue('a@b.com');
  103. form.password.$setViewValue('my_password12345');
  104. form.confirmation.$setViewValue('my_password12345');
  105. form.agreement.$setViewValue(true);
  106. return scope.$digest();
  107. });
  108. it('should call createWallet()', inject(function ($timeout) {
  109. spyOn(scope, 'createWallet');
  110. scope.signup();
  111. scope.$digest();
  112. $timeout.flush();
  113. expect(scope.createWallet).toHaveBeenCalled();
  114. })
  115. );
  116. it('should not call createWallet() if validation failed', () => {
  117. spyOn(scope, 'createWallet');
  118. scope.signupForm.password.$setViewValue('weak');
  119. scope.$digest();
  120. scope.signup();
  121. expect(scope.createWallet).not.toHaveBeenCalled();
  122. });
  123. it('should create a new wallet', inject(function (Wallet) {
  124. spyOn(Wallet, 'create');
  125. scope.createWallet((function () { }));
  126. expect(Wallet.create).toHaveBeenCalled();
  127. })
  128. );
  129. it('should add password to local storage in dev mode', inject(function (localStorageService) {
  130. spyOn(localStorageService, 'set');
  131. scope.autoReload = true;
  132. scope.fields.password = "testing";
  133. scope.signup();
  134. scope.$digest();
  135. expect(localStorageService.set).toHaveBeenCalledWith('password', "testing");
  136. })
  137. );
  138. it('should not add password to local storage in production mode', inject(function (localStorageService) {
  139. spyOn(localStorageService, 'set');
  140. scope.autoReload = false;
  141. scope.fields.password = "testing";
  142. scope.signup();
  143. expect(localStorageService.set).not.toHaveBeenCalledWith('password', "testing");
  144. })
  145. );
  146. });
  147. describe('language', () => {
  148. it('should guess the correct language', () => expect(scope.language_guess.code).toBe("en"));
  149. it('should switch interface language to guessed language', inject(function ($translate, languages) {
  150. spyOn($translate, "use");
  151. expect(scope.language_guess.code).not.toBe(languages.languages[0].code);
  152. scope.language_guess = languages.languages[0];
  153. scope.$digest();
  154. expect($translate.use).toHaveBeenCalledWith(languages.languages[0].code);
  155. })
  156. );
  157. });
  158. describe('currency', () =>
  159. it('should guess the correct currency', () => {
  160. $httpBackend.flush();
  161. expect(scope.currency_guess.code).toBe("EUR");
  162. })
  163. );
  164. });