Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Angular: remove comments in tests #10417

Merged
merged 1 commit into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,11 @@ describe('HealthComponent', () => {

describe('refresh', () => {
it('should call refresh on init', () => {
// GIVEN
const health: Health = { status: 'UP', components: { mail: { status: 'UP', details: { mailDetail: 'mail' } } } };
jest.spyOn(service, 'checkHealth').mockReturnValue(of(health));

// WHEN
comp.ngOnInit();

// THEN
expect(service.checkHealth).toHaveBeenCalled();
expect(
service.checkHealth().subscribe({
Expand All @@ -50,50 +47,40 @@ describe('HealthComponent', () => {
});

it('should call checkHealth on refresh', done => {
// GIVEN
const health: Health = { status: 'UP', components: { mail: { status: 'UP', details: { mailDetail: 'mail' } } } };
jest.spyOn(service, 'checkHealth').mockImplementation(() => of(health));

// WHEN
comp.refresh();
done();

// THEN
expect(service.checkHealth).toHaveBeenCalled();
});
});

describe('showHealth', () => {
it('should open dialog', async () => {
// GIVEN
const health: Health = {
status: 'UP',
components: null,
};

// WHEN
comp.showHealth(health);

// THEN
const dialogs = await loader.getAllHarnesses(MatDialogHarness);
expect(dialogs.length).toBe(1);
});
});

describe('getBadgeClass', () => {
it('should get success badge class', () => {
// WHEN
const upBadgeClass = comp.getBadgeClass('UP');

// THEN
expect(upBadgeClass).toEqual('bg-success');
});

it('should get danger badge class', () => {
// WHEN
const downBadgeClass = comp.getBadgeClass('DOWN');

// THEN
expect(downBadgeClass).toEqual('bg-danger');
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ describe('HealthService Service', () => {

describe('Service methods', () => {
it('should call management/health endpoint with correct values', () => {
// GIVEN
let expectedResult;
const checkHealth = {
components: [],
};

// WHEN
service.checkHealth().subscribe(received => {
expectedResult = received;
});
Expand All @@ -42,7 +40,6 @@ describe('HealthService Service', () => {
});
testRequest.flush(checkHealth);

// THEN
expect(expectedResult).toEqual(checkHealth);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,72 +31,57 @@ describe('HealthModalComponent', () => {

describe('readableValue', () => {
it('should return stringify value', () => {
// GIVEN
comp.healthDetails = {
key: 'hystrix',
status: 'UP',
};

// WHEN
const result = comp.readableValue({ name: 'jhipster' });

// THEN
expect(result).toEqual('{"name":"jhipster"}');
});

it('should return string value', () => {
// GIVEN
comp.healthDetails = {
key: 'hystrix',
status: 'UP',
};

// WHEN
const result = comp.readableValue('jhipster');

// THEN
expect(result).toEqual('jhipster');
});

it('should return storage space in an human readable unit (GB)', () => {
// GIVEN
comp.healthDetails = {
key: 'diskSpace',
status: 'UP',
};

// WHEN
const result = comp.readableValue(1073741825);

// THEN
expect(result).toEqual('1.00 GB');
});

it('should return storage space in an human readable unit (MB)', () => {
// GIVEN
comp.healthDetails = {
key: 'diskSpace',
status: 'UP',
};

// WHEN
const result = comp.readableValue(1073741824);

// THEN
expect(result).toEqual('1024.00 MB');
});

it('should return string value', () => {
// GIVEN
comp.healthDetails = {
key: 'mail',
status: 'UP',
};

// WHEN
const result = comp.readableValue(1234);

// THEN
expect(result).toEqual('1234');
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ describe('App Component', () => {

describe('ngOnInit', () => {
it('should have appName', () => {
// WHEN
fixture.detectChanges();

// THEN
expect(comp.appName()).toEqual('{{baseName}}');
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Account } from './account.model';

describe('AccountModel', () => {
it('should initialize an account model', () => {
// GIVEN
const activated = true;
const authorities = ['ADMIN'];
const email = 'jdoe@test.com';
Expand All @@ -11,10 +10,8 @@ describe('AccountModel', () => {
const lastName = 'Doe';
const login = 'jdoe';

// WHEN
const account: Account = new Account(activated, authorities, email, firstName, langKey, lastName, login);

// THEN
expect(account.activated).toBe(activated);
expect(account.authorities).toBe(authorities);
expect(account.email).toBe(email);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,69 +43,53 @@ describe('Account Service', () => {

describe('authenticate', () => {
it('authenticationState should emit null if input is null', () => {
// GIVEN
let userIdentity: Account | null = accountWithAuthorities([]);
service.getAuthenticationState().subscribe(account => (userIdentity = account));

// WHEN
service.authenticate(null);

// THEN
expect(userIdentity).toBeNull();
expect(service.isAuthenticated()).toBe(false);
});

it('authenticationState should emit the same account as was in input parameter', () => {
// GIVEN
const expectedResult = accountWithAuthorities([]);
let userIdentity: Account | null = null;
service.getAuthenticationState().subscribe(account => (userIdentity = account));

// WHEN
service.authenticate(expectedResult);

// THEN
expect(userIdentity).toEqual(expectedResult);
});
});

describe('identity', () => {
it('should handle error when /account is on error', () => {
// When I call
service.identity(false).subscribe();

// Then there is a new error request
httpMock.expectOne({ method: 'GET' }).flush('404 error', { status: 404, statusText: 'Not Found' });
});

it('should call /account only once if last call have not returned', () => {
// When I call
service.identity().subscribe();

// Once more
service.identity().subscribe();

// Then there is only request
httpMock.expectOne({ method: 'GET' });
});

it('should call /account only once if not logged out after first authentication and should call /account again if user has logged out', () => {
// Given the user is authenticated
service.identity().subscribe();
httpMock.expectOne({ method: 'GET' }).flush({});

// When I call
service.identity().subscribe();

// Then there is no second request
httpMock.expectNone({ method: 'GET' });

// When I log out
service.authenticate(null);
// and then call

service.identity().subscribe();

// Then there is a new request
httpMock.expectOne({ method: 'GET' });
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,22 @@ describe('Auth JWT', () => {

describe('Login', () => {
it('should clear session storage and save in local storage', () => {
// GIVEN
jest.spyOn(Storage.prototype, 'setItem');

// WHEN
service.login({ username: 'John', password: '123' }).subscribe();
httpMock.expectOne('api/authenticate').flush({ id_token: '1' });

// THEN
httpMock.verify();
expect(localStorage.setItem).toHaveBeenCalledWith('authenticationToken', '1');
});
});

describe('Logout', () => {
it('should clear storage', () => {
// GIVEN
jest.spyOn(Storage.prototype, 'removeItem');

// WHEN
service.logout().subscribe();

// THEN
expect(localStorage.removeItem).toHaveBeenCalledWith('authenticationToken');
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@ describe('Auth Interceptor', () => {

describe('intercept', () => {
it('should add authorization header with localStorageService', () => {
// GIVEN
const token = 'azerty';
Storage.prototype.getItem = jest.fn(() => token);

// WHEN
service.login({ username: 'John', password: '123' }).subscribe();

// THEN
const httpReq = httpMock.expectOne('api/authenticate');
expect(httpReq.request.headers.has('Authorization')).toBe(true);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,36 +53,27 @@ describe('Login Component', () => {

describe('ngOnInit', () => {
it('should check authentication', () => {
// GIVEN
const authenticationState = new Subject<Account | null>();
mockAccountService.getAuthenticationState = jest.fn(() => authenticationState.asObservable());

// WHEN
comp.ngOnInit();

// THEN
expect(mockAccountService.getAuthenticationState).toHaveBeenCalled();

// THEN
expect(comp.account()).toBeNull();

// WHEN
authenticationState.next(account);

// THEN
expect(comp.account()).toEqual(account);

// WHEN
authenticationState.next(null);

// THEN
expect(comp.account()).toBeNull();
});
});

describe('login', () => {
it('should authenticate the user', () => {
// GIVEN
const credentials = {
username: 'admin',
password: 'admin',
Expand All @@ -93,32 +84,25 @@ describe('Login Component', () => {
password: 'admin',
});

// WHEN
comp.login();

// THEN
expect(mockLoginService.login).toHaveBeenCalledWith(credentials);
});

it('should not authenticate the user', () => {
// GIVEN
const err = { status: 403 };
jest.spyOn(mockLoginService, 'login').mockReturnValue(throwError(() => err));

// WHEN
comp.login();

// THEN
expect(comp.errorMessage()).toEqual('Authentication failed');
});
});

describe('logout', () => {
it('should logout the user', () => {
// WHEN
comp.logout();

// THEN
expect(mockLoginService.logout).toHaveBeenCalled();
});
});
Expand Down
Loading