Skip to content

Commit

Permalink
124 make use of @__PURE__ (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinMeow authored Apr 1, 2024
2 parents e359346 + 73e3f72 commit b4e2399
Show file tree
Hide file tree
Showing 18 changed files with 60 additions and 45 deletions.
6 changes: 3 additions & 3 deletions client/cypress/e2e/auth/auth-corner.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ describe('auth-corner should', () => {
})

it('navigate to login on login button click', () => {
cy.getByAttr('login-button').click()
cy.byTestAttr('login-button').click()
cy.url().should('not.include', 'register')
cy.url().should('include', 'login')
})

it('navigate to register on register button click', () => {
cy.getByAttr('register-button').click()
cy.byTestAttr('register-button').click()
cy.url().should('not.include', 'login')
cy.url().should('include', 'register')
})

it('contain logout button', () => {
cy.createTestUser()

cy.getByAttr('logout-button')
cy.byTestAttr('logout-button')
.should('be.visible')
.invoke('text')
.should('have.length.above', 1)
Expand Down
24 changes: 12 additions & 12 deletions client/cypress/e2e/auth/auth.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ describe('auth should', () => {
chefname: 'Cypress-Register',
password: 'iLoveJesus<3'
}
cy.getByAttr('register-submit-button').as('submit-btn')
cy.byTestAttr('register-submit-button').as('submit-btn')

cy.get('@submit-btn').should('be.disabled')

cy.getByAttr('register-name-input').type(credentials.chefname)
cy.getByAttr('password-input').type(credentials.password)
cy.byTestAttr('register-name-input').type(credentials.chefname)
cy.byTestAttr('password-input').type(credentials.password)

cy.get('@submit-btn').should('be.enabled')
cy.intercept({
Expand All @@ -24,30 +24,30 @@ describe('auth should', () => {
cy.url().should('not.include', 'register')
cy.get('auth-corner')
.as('authc')
.getByAttr('logout-button')
.byTestAttr('logout-button')
.contains('Ausloggen')

// Logout
cy.get('@authc').getByAttr('logout-button').click()
cy.get('@authc').byTestAttr('logout-button').click()

// Login
cy.visit('/login')
cy.getByAttr('login-name-input').type(credentials.chefname)
cy.getByAttr('password-input').type(credentials.password)
cy.getByAttr('login-submit-button').click() // should redirect somewhere in success
cy.byTestAttr('login-name-input').type(credentials.chefname)
cy.byTestAttr('password-input').type(credentials.password)
cy.byTestAttr('login-submit-button').click() // should redirect somewhere in success
cy.url().should('not.include', 'login')
cy.get('@authc').getByAttr('logout-button').contains('Ausloggen')
cy.get('@authc').byTestAttr('logout-button').contains('Ausloggen')

// Delete the just registered account
cy.visit('/delete-chef')
cy.getByAttr('password-input').type(credentials.password)
cy.byTestAttr('password-input').type(credentials.password)
cy.intercept({
path: '/Auth/DeleteAsync',
times: 1
}).as('delete-http-request')
cy.getByAttr('delete-chef-form').submit() // redirect on success
cy.byTestAttr('delete-chef-form').submit() // redirect on success
cy.wait('@delete-http-request')
cy.url().should('not.include', 'delete-chef')
cy.get('@authc').getByAttr('login-button')
cy.get('@authc').byTestAttr('login-button')
})
})
2 changes: 1 addition & 1 deletion client/cypress/e2e/auth/delete-account.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('delete-chef should', () => {

cy.visit('/delete-chef')

cy.getByAttr('password-input').type('wrong-password{enter}')
cy.byTestAttr('password-input').type('wrong-password{enter}')

cy.url().should('include', 'delete-chef')

Expand Down
2 changes: 1 addition & 1 deletion client/cypress/e2e/auth/login.cy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
describe('login should', () => {
it('not log in with empty inputs', () => {
cy.visit('/login')
cy.getByAttr('login-submit-button').click({ force: true })
cy.byTestAttr('login-submit-button').click({ force: true })
cy.url().should('include', 'login')
})
})
Expand Down
6 changes: 3 additions & 3 deletions client/cypress/e2e/header/header.cy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function getOpenMenuButton() {
return cy.getByAttr('open-menu-button')
return cy.byTestAttr('open-menu-button')
}

describe('header should', () => {
Expand All @@ -12,7 +12,7 @@ describe('header should', () => {
// Arrange
cy.visit('/')
cy.get('core-menu').as('menu').should('be.visible')
cy.getByAttr('close-menu-button').click()
cy.byTestAttr('close-menu-button').click()

// Assert
cy.get('@menu').should('not.be.visible')
Expand All @@ -23,7 +23,7 @@ describe('header should', () => {
// redirect
it('redirect to home when app title is clicked', () => {
cy.visit('/login')
cy.getByAttr('app-title').click()
cy.byTestAttr('app-title').click()
cy.url().should('not.include', 'login')
})
})
2 changes: 1 addition & 1 deletion client/cypress/e2e/menu/menu.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ describe('menu should', () => {
it('close on close-menu-button click', () => {
cy.visit('/')
cy.get('core-menu').as('menu').should('be.visible')
cy.getByAttr('close-menu-button').click()
cy.byTestAttr('close-menu-button').click()
cy.get('@menu').should('not.be.visible')
})
})
4 changes: 2 additions & 2 deletions client/cypress/e2e/recipe/create-recipe.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ describe('create-recipe should', () => {

const recipeTitle = 'valid recipe title'

cy.getByAttr('recipe-title-input').type(recipeTitle)
cy.byTestAttr('recipe-title-input').type(recipeTitle)

cy.intercept({
path: '/Recipe/AddAsync',
times: 1
}).as('create-recipe')

cy.getByAttr('create-recipe-submit-button').click()
cy.byTestAttr('create-recipe-submit-button').click()

cy.wait('@create-recipe').then((stuff) => {
const newRecipe: { id: string } = stuff.response?.body
Expand Down
6 changes: 3 additions & 3 deletions client/cypress/e2e/recipe/view-recipe.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ describe('view-recipe should', () => {

const recipeTitle = 'valid recipe title'

cy.getByAttr('recipe-title-input').type(recipeTitle)
cy.byTestAttr('recipe-title-input').type(recipeTitle)

cy.intercept({
path: '/Recipe/AddAsync',
times: 1
}).as('create-recipe')

cy.getByAttr('create-recipe-submit-button').click()
cy.byTestAttr('create-recipe-submit-button').click()

cy.wait('@create-recipe').then((stuff) => {
const newRecipe: { id: string } = stuff.response?.body
cy.url().should('include', 'recipe/' + newRecipe.id)
})

cy.getByAttr('title').contains(recipeTitle)
cy.byTestAttr('title').contains(recipeTitle)

cy.deleteTestUser()
})
Expand Down
18 changes: 10 additions & 8 deletions client/cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

declare namespace Cypress {
interface Chainable<Subject = any> {
getByAttr(
byTestAttr(
selector: string,
options?:
| Partial<
Expand All @@ -68,8 +68,10 @@ declare namespace Cypress {
* @example
* <button data-test-my-button></button>
* Cy.getByDataCy('my-button').should.be('be.visivle');
*
* @__PURE__
*/
function getByAttr(
function byTestAttr(
selector: string,
options?:
| Partial<
Expand All @@ -83,7 +85,7 @@ function getByAttr(
return cy.get(`[data-test-${selector}]`)
}

Cypress.Commands.add('getByAttr', getByAttr)
Cypress.Commands.add('byTestAttr', byTestAttr)

function createTestUser() {
cy.fixture('test-user.json').as('user')
Expand All @@ -98,15 +100,15 @@ function createTestUser() {
}
})

cy.getByAttr('register-name-input').type(chefname)
cy.getByAttr('password-input').type(password)
cy.byTestAttr('register-name-input').type(chefname)
cy.byTestAttr('password-input').type(password)

cy.intercept({
path: '/Auth/RegisterAsync',
times: 1
}).as('registerAsync')

cy.getByAttr('register-form').submit()
cy.byTestAttr('register-form').submit()

cy.wait('@registerAsync')
// I dont know why, but this is required, else the "login redirects when logged in already" - test fails
Expand All @@ -122,12 +124,12 @@ function deleteTestUser() {
cy.get('@user').then((user) => {
const { password }: any = user
cy.visit('/delete-chef')
cy.getByAttr('password-input').type(password)
cy.byTestAttr('password-input').type(password)
cy.intercept({
path: '/Auth/DeleteAsync',
times: 1
}).as('deleteAsync')
cy.getByAttr('delete-chef-form').submit()
cy.byTestAttr('delete-chef-form').submit()
cy.wait('@deleteAsync')
})
}
Expand Down
2 changes: 1 addition & 1 deletion client/src/app/app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { provideHttpClientTesting } from '@angular/common/http/testing'
import { provideHttpClient } from '@angular/common/http'
import { Header, provideAppName } from 'src/app/core'
import { provideNoopAnimations } from '@angular/platform-browser/animations'
import { provideApiBaseUrlTesting } from './shared/api/provideApiBaseUrl'
import { provideApiBaseUrlTesting } from './shared/api/provideApiBaseUrlTesting'
import { provideRouter } from '@angular/router'
import { MockComponent } from 'ng-mocks'
import { MatDrawer } from '@angular/material/sidenav'
Expand Down
4 changes: 3 additions & 1 deletion client/src/app/auth/utils/unauthorized.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { inject } from '@angular/core'
import { CanActivateFn, Router } from '@angular/router'
import { AuthService } from './auth.service'

/** redirects to home, if authorized to protect the route which is reserved for unauthorized users. Like login. */
/**
* redirects to home, if authorized to protect the route which is reserved for unauthorized users. Like login.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export const unauthorizedGuard: CanActivateFn = (route, state) => {
const router = inject(Router)
Expand Down
2 changes: 2 additions & 0 deletions client/src/app/common/assertions/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
* const value: { name: string; } | null = null
* assert(value !== null, 'Value may not be null.') // will throw an error
* console.log(value.name); // Non-null assertion operator not required onwards
*
* @__PURE__
*/
export function assert(condition: unknown, msg: string): asserts condition {
if (!condition) throw new Error(msg)
Expand Down
2 changes: 2 additions & 0 deletions client/src/app/common/testing/byTestAttr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { ComponentFixture } from '@angular/core/testing'
* // alternatively: <div data-test="example"></div>
* const div = queryByTestAttr<HTMLButtonElement>(fixture, 'example');
* expect(div)toBeTruthy();
*
* @__PURE__
*/
export function byTestAttr<T>(
fixture: ComponentFixture<unknown>,
Expand Down
2 changes: 2 additions & 0 deletions client/src/app/common/testing/findComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { By } from '@angular/platform-browser'
* const fixture = TestBed.createComponent(ParentComponent);
* fixture.detectChanges();
* const childComponent = findComponent(fixture, ChildComponent);
*
* @__PURE__
*/
export function findComponent<T>(
fixture: ComponentFixture<unknown>,
Expand Down
2 changes: 2 additions & 0 deletions client/src/app/common/testing/setValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* @example
* const inputElement = document.querySelector('input[type="text"]');
* setValue(inputElement, 'New Value');
*
* @__PURE__
*/
export function setValue(input: HTMLInputElement, newValue: string) {
input.value = newValue
Expand Down
1 change: 1 addition & 0 deletions client/src/app/shared/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './API-BASE-URL'
export * from './base.api'
export * from './provideApiBaseUrl'
export * from './provideApiBaseUrlTesting'
9 changes: 0 additions & 9 deletions client/src/app/shared/api/provideApiBaseUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,3 @@ export const provideApiBaseUrl = function (): EnvironmentProviders {
}
])
}

export const provideApiBaseUrlTesting = function (): EnvironmentProviders {
return makeEnvironmentProviders([
{
provide: API_BASE_URL,
useValue: 'fake-url'
}
])
}
11 changes: 11 additions & 0 deletions client/src/app/shared/api/provideApiBaseUrlTesting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { EnvironmentProviders, makeEnvironmentProviders } from '@angular/core'
import { API_BASE_URL } from './API-BASE-URL'

export const provideApiBaseUrlTesting = function (): EnvironmentProviders {
return makeEnvironmentProviders([
{
provide: API_BASE_URL,
useValue: 'fake-url'
}
])
}

0 comments on commit b4e2399

Please sign in to comment.