register.component.spec.ts
3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { ComponentTestHelper, createClass } from "../../spec/component-test-helper";
import * as helpers from "../../spec/helpers";
import { RegisterComponent } from "./register.component";
describe("Register Component", () => {
const htmlTemplate: string = '<noosfero-register></noosfero-register>';
let helper: ComponentTestHelper<RegisterComponent>;
let registerService = helpers.mocks.registerService;
let stateService: angular.ui.IStateService;
let notificationService = helpers.mocks.notificationService;
notificationService.success = jasmine.createSpy('success');
notificationService.error = jasmine.createSpy('error');
let user_data: any;
let response: any;
let deferred: any;
let $rootScope: ng.IRootScopeService;
let $q: ng.IQService;
beforeEach(() => {
stateService = jasmine.createSpyObj("$state", ["transitionTo"]);
angular.mock.module('templates');
angular.mock.module('ngSanitize');
angular.mock.module('ngMessages');
angular.mock.module('ngPassword');
});
beforeEach((done) => {
let cls = createClass({
template: htmlTemplate,
directives: [RegisterComponent],
providers: [
helpers.createProviderToValue('$state', stateService),
helpers.createProviderToValue('$uibModal', helpers.mocks.$modal),
helpers.createProviderToValue('RegisterService', registerService),
helpers.createProviderToValue('NotificationService', notificationService),
helpers.createProviderToValue('EnvironmentService', helpers.mocks.environmentService)
]
});
helper = new ComponentTestHelper<RegisterComponent>(cls, done);
});
beforeEach(inject((_$rootScope_: ng.IRootScopeService, _$q_: ng.IQService) => {
$rootScope = _$rootScope_;
$q = _$q_;
}));
it('register page was rendered', () => {
expect(helper.debugElement.query('div.register-page').length).toEqual(1);
});
it("registers a new user", done => {
user_data = { username: "username", password: "password", password_confirmation: "password", email: "user@company.com" };
response = {};
helper.component.account = user_data;
deferred = $q.defer();
deferred.resolve({ data: response });
registerService.createAccount = jasmine.createSpy("createAccount").and.returnValue(deferred.promise);
helper.component.signup();
helper.detectChanges();
expect(registerService.createAccount).toHaveBeenCalledWith(user_data);
expect(stateService.transitionTo).toHaveBeenCalledWith("main.environment");
expect(notificationService.success).toHaveBeenCalled();
done();
});
it("gives error when registration fails", done => {
user_data = { password: "pas" };
response = { data: { message: '{ "password": ["is too short"] }' } };
helper.component.account = user_data;
deferred = $q.defer();
deferred.reject(response);
registerService.createAccount = jasmine.createSpy("createAccount").and.returnValue(deferred.promise);
helper.component.signup();
helper.detectChanges();
expect(registerService.createAccount).toHaveBeenCalledWith(user_data);
expect(stateService.transitionTo).not.toHaveBeenCalledWith("main.environment");
expect(notificationService.error).toHaveBeenCalled();
done();
});
});