Skip to content

Instantly share code, notes, and snippets.

View johannesnormannjensen's full-sized avatar

Johannes Normann Jensen johannesnormannjensen

View GitHub Profile
Briebug repository: https://github.com/briebug/jest-schematic
`ng add @briebug/jest-schematic`
when migrating to jest be sure to:
run npm install jest-preset-angular
import 'jest-preset-angular/setup-jest'; <-in the top of setup-jest.ts file
@johannesnormannjensen
johannesnormannjensen / login.component.ts
Last active March 29, 2023 21:21
LoginModule - standalone
import { CommonModule } from '@angular/common';
import { Component } from '@angular/core';
import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { ButtonModule } from '../button/button.component';
import { AuthService } from '../service/auth.service';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
@johannesnormannjensen
johannesnormannjensen / auth.service.spec.ts
Last active September 16, 2024 22:28
AuthService - Without TestBed
import { AuthService } from './auth.service';
describe('AuthService', () => {
let service: AuthService;
beforeEach(() => {
service = new AuthService();
});
it("should login with email & password", () => {
@johannesnormannjensen
johannesnormannjensen / auth.service.spec.ts
Last active September 16, 2024 22:52
AuthService - should call '/api/login'
it("should call '/api/login'", (done) => {
// Arrange
const email = 'email@email.com';
const password = 'password';
const login$ = service.login(email, password);
// Act
login$.subscribe({
next: () => {
expect(httpClientMock.post).toHaveBeenCalledWith('/api/login', { email, password });
done();
@johannesnormannjensen
johannesnormannjensen / auth.service.ts
Last active December 8, 2024 22:18
AuthService - Asynchronous login
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { BehaviorSubject, first, map, Observable, shareReplay, switchMap } from 'rxjs';
import { AuthUser } from '../model/auth-user';
import { isNullOrUndefined } from '../utils/object-utils';
@Injectable({
providedIn: 'root'
})
export class AuthService {
@johannesnormannjensen
johannesnormannjensen / auth.service.spec.ts
Last active September 16, 2024 22:53
AuthService - Asynchronous testing
import { firstValueFrom, of } from 'rxjs';
import { first } from 'rxjs/operators';
import { AuthService } from './auth.service';
describe('AuthService', () => {
describe("Always", () => {
let service: AuthService;
beforeEach(() => {
@johannesnormannjensen
johannesnormannjensen / login.component.spec.ts
Last active April 2, 2025 21:02
LoginComponent - calls login & will not call login
import { provideHttpClient } from '@angular/common/http';
import { provideHttpClientTesting } from '@angular/common/http/testing';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { Router } from '@angular/router';
import { firstValueFrom, of } from 'rxjs';
import { AuthService } from '../service/auth.service';
import { LoginComponent } from './login.component';
describe('LoginComponent', () => {
let component: LoginComponent;
@johannesnormannjensen
johannesnormannjensen / login.component.spec.ts
Last active September 16, 2024 23:02
LoginComponent - it calls login with email & password from form
it('should login with email & password from form', () => {
// Arrange
const email = 'email@email.com';
const password = 'pword';
const authService = TestBed.inject(AuthService);
const authSpy = jest.spyOn(authService, 'login');
// Act
component.fg.patchValue({ email, password });
component.login();
@johannesnormannjensen
johannesnormannjensen / login.component.html
Last active September 17, 2024 11:58
Initial LoginComponent
<div class="d-flex align-items-center h-100">
<div class="card form-signin">
<form [formGroup]="fg" (submit)="login()">
<h1 class="h3 mb-3 font-weight-normal">Please sign in</h1>
<input formControlName="email" type="email" class="form-control" placeholder="Email address" autofocus="">
<input formControlName="password" type="password" class="mt-2 mb-3 form-control" placeholder="Password">
<button class="mt-5 btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>
</div>
@johannesnormannjensen
johannesnormannjensen / auth.service.spec.ts
Last active November 16, 2022 20:21
AuthService - the unhappy path
import { AuthService } from './auth.service';
describe('AuthService', () => {
describe("Always", () => {
let service: AuthService;
beforeEach(() => {
service = new AuthService();
});