選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

124 行
3.8KB

  1. import { ComponentFixture, TestBed, fakeAsync, inject, tick, waitForAsync } from '@angular/core/testing';
  2. import { provideHttpClient } from '@angular/common/http';
  3. import { FormBuilder } from '@angular/forms';
  4. import { of, throwError } from 'rxjs';
  5. import { TranslateModule, TranslateService } from '@ngx-translate/core';
  6. import { EMAIL_ALREADY_USED_TYPE, LOGIN_ALREADY_USED_TYPE } from 'app/config/error.constants';
  7. import { RegisterService } from './register.service';
  8. import RegisterComponent from './register.component';
  9. describe('RegisterComponent', () => {
  10. let fixture: ComponentFixture<RegisterComponent>;
  11. let comp: RegisterComponent;
  12. beforeEach(waitForAsync(() => {
  13. TestBed.configureTestingModule({
  14. imports: [TranslateModule.forRoot(), RegisterComponent],
  15. providers: [provideHttpClient(), FormBuilder],
  16. })
  17. .overrideTemplate(RegisterComponent, '')
  18. .compileComponents();
  19. }));
  20. beforeEach(() => {
  21. fixture = TestBed.createComponent(RegisterComponent);
  22. comp = fixture.componentInstance;
  23. });
  24. it('should ensure the two passwords entered match', () => {
  25. comp.registerForm.patchValue({
  26. password: 'password',
  27. confirmPassword: 'non-matching',
  28. });
  29. comp.register();
  30. expect(comp.doNotMatch()).toBe(true);
  31. });
  32. it('should update success to true after creating an account', inject(
  33. [RegisterService, TranslateService],
  34. fakeAsync((service: RegisterService, mockTranslateService: TranslateService) => {
  35. jest.spyOn(service, 'save').mockReturnValue(of({}));
  36. mockTranslateService.currentLang = 'fr';
  37. comp.registerForm.patchValue({
  38. password: 'password',
  39. confirmPassword: 'password',
  40. });
  41. comp.register();
  42. tick();
  43. expect(service.save).toHaveBeenCalledWith({
  44. email: '',
  45. password: 'password',
  46. login: '',
  47. langKey: 'fr',
  48. });
  49. expect(comp.success()).toBe(true);
  50. expect(comp.errorUserExists()).toBe(false);
  51. expect(comp.errorEmailExists()).toBe(false);
  52. expect(comp.error()).toBe(false);
  53. }),
  54. ));
  55. it('should notify of user existence upon 400/login already in use', inject(
  56. [RegisterService],
  57. fakeAsync((service: RegisterService) => {
  58. const err = { status: 400, error: { type: LOGIN_ALREADY_USED_TYPE } };
  59. jest.spyOn(service, 'save').mockReturnValue(throwError(() => err));
  60. comp.registerForm.patchValue({
  61. password: 'password',
  62. confirmPassword: 'password',
  63. });
  64. comp.register();
  65. tick();
  66. expect(comp.errorUserExists()).toBe(true);
  67. expect(comp.errorEmailExists()).toBe(false);
  68. expect(comp.error()).toBe(false);
  69. }),
  70. ));
  71. it('should notify of email existence upon 400/email address already in use', inject(
  72. [RegisterService],
  73. fakeAsync((service: RegisterService) => {
  74. const err = { status: 400, error: { type: EMAIL_ALREADY_USED_TYPE } };
  75. jest.spyOn(service, 'save').mockReturnValue(throwError(() => err));
  76. comp.registerForm.patchValue({
  77. password: 'password',
  78. confirmPassword: 'password',
  79. });
  80. comp.register();
  81. tick();
  82. expect(comp.errorEmailExists()).toBe(true);
  83. expect(comp.errorUserExists()).toBe(false);
  84. expect(comp.error()).toBe(false);
  85. }),
  86. ));
  87. it('should notify of generic error', inject(
  88. [RegisterService],
  89. fakeAsync((service: RegisterService) => {
  90. const err = { status: 503 };
  91. jest.spyOn(service, 'save').mockReturnValue(throwError(() => err));
  92. comp.registerForm.patchValue({
  93. password: 'password',
  94. confirmPassword: 'password',
  95. });
  96. comp.register();
  97. tick();
  98. expect(comp.errorUserExists()).toBe(false);
  99. expect(comp.errorEmailExists()).toBe(false);
  100. expect(comp.error()).toBe(true);
  101. }),
  102. ));
  103. });