Skip to content

Commit

Permalink
fix: improve quality
Browse files Browse the repository at this point in the history
- fix formatting
- add tests
  • Loading branch information
cgawron committed Oct 16, 2024
1 parent 0659aac commit bd641ff
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 16 deletions.
66 changes: 61 additions & 5 deletions client/cypress/e2e/app.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,34 @@ context('User not logged in', () => {
cy.intercept('https://accounts.google.com/gsi/button', {
statusCode: 200, body: {}
}).as('googleButton')
})

it('Should redirect to landing', () => {
cy.visit('/app')
cy.wait(['@getUser'], { timeout: 10000 })
cy.location('pathname').should('eq', '/landing')
})

it('Should redirect to landing', () => {
cy.wait(['@getUser'], { timeout: 10000 })
cy.intercept('/api/v1/users/user', {
statusCode: 401, body: { "success": false, "message": "Unauthorized! Sign in again!" }
}).as('getUser1')
cy.visit('/app')
cy.wait(['@getUser1'], { timeout: 10000 })
cy.location('pathname').should('eq', '/landing')
})

it('Should redirect to landing', () => {
cy.intercept('/api/v1/users/user', {
statusCode: 500
}).as('getUser2')
cy.visit('/app')
cy.wait(['@getUser2'], { timeout: 10000 })
cy.location('pathname').should('eq', '/landing')
})

it('should show login button', () => {
cy.visit('/app')
cy.wait(['@getUser'], { timeout: 10000 })
cy.location('pathname').should('eq', '/landing')
cy.get('[data-testid="profile-menu"]').click()
Expand All @@ -33,22 +51,23 @@ context('Main page', () => {
})

describe('Visit app main page & add event type', () => {
before(() => {
beforeEach(() => {
cy.intercept('/api/v1/users/user', { fixture: 'user' }).as('getUser')
cy.intercept('/api/v1/events/getEvents', { fixture: 'events' }).as('getEvents')
cy.intercept('/api/v1/events/addEvent', { fixture: 'addEvent' }).as('addEvent')
cy.intercept('/api/v1/events/getEvents', { fixture: 'eventsAfter' }).as('getEventsAfter')
cy.intercept('DELETE', '/api/v1/events/deleteEvent/670eca0bc1eebcf903b17528', {
statusCode: 200, body: { "msg": "Successfully deleted the Event" }
}).as('deleteEvent')
})

it('Check add event type', () => {
it('Check add/delete event type', () => {
cy.visit('/app')
cy.wait(['@getUser'], { timeout: 10000 })
cy.wait(['@getUser', '@getEvents'], { timeout: 10000 })
cy.intercept('/api/v1/events/getEvents', { fixture: 'eventsAfter' }).as('getEventsAfter')
cy.get('[data-testid="add-event-button"]').click()
cy.get('[data-testid="event-form-title"]').type('Test event')
cy.get('[data-testid="event-form-submit"]').click()

cy.wait(['@addEvent', '@getEventsAfter'], { timeout: 10000 })
cy.get('[data-testid="copy-link-button"]').last().click({ force: true })

Expand All @@ -57,6 +76,43 @@ context('Main page', () => {
cy.get('[data-testid="event-card"]').should('have.length', 1)
cy.get('[data-testid="event-card"]').contains('Test event').should('not.exist')
})

it('Check delete error handling', () => {
cy.intercept('DELETE', '/api/v1/events/deleteEvent/66e41e641f4f81ece1828ab5', {
statusCode: 400, body: { "msg": "Successfully deleted the Event" }
}).as('deleteEvent')
cy.visit('/app')
cy.wait(['@getUser'], { timeout: 10000 })
cy.wait(['@getEvents'], { timeout: 10000 })

cy.get('[data-testid="delete-event-button"]').click()
cy.wait(['@deleteEvent'], { timeout: 10000 })
})
})

describe('Visit app main page & disable event', () => {
before(() => {
cy.intercept('/api/v1/users/user', { fixture: 'user' }).as('getUser')
cy.intercept('/api/v1/events/getEvents', { fixture: 'events' }).as('getEvents')
cy.intercept('GET', '/api/v1/events/getEvent/66e41e641f4f81ece1828ab5', { fixture: 'sprechstunde' }).as('getEvent')
cy.intercept('PUT', '/api/v1/events/updateEvent/66e41e641f4f81ece1828ab5', { fixture: 'sprechstunde' }).as('putEvent')
})

it('Check event actions', () => {
cy.visit('/app')
cy.wait(['@getUser'], { timeout: 10000 })
cy.wait(['@getEvents'], { timeout: 10000 })
cy.get('[data-testid="active-switch"]').click()
cy.wait(['@putEvent'], { timeout: 10000 })
cy.get('[data-testid="event-card"]').should('have.class', 'inactive')
cy.get('[data-testid="active-switch"]').click()
cy.wait(['@putEvent'], { timeout: 10000 })
cy.get('[data-testid="event-card"]').should('have.class', 'active')
cy.get('[data-testid="copy-link-button"]').click()
cy.window().its('navigator.clipboard')
.then((clip) => clip.readText())
.should((s) => s.endsWith('users/christian-gawron/sprechstunde'))
})
})

describe('Visit app main page & edit event type', () => {
Expand Down
4 changes: 2 additions & 2 deletions client/src/components/EventCard.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.inactive {
color: rgb(196, 194, 194);
.MuiCard-root.inactive {
color: rgb(219, 219, 219);
}
6 changes: 4 additions & 2 deletions client/src/components/EventCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import "./EventCard.css";
type EventCardProps = {
event: EventDocument;
url: string;
setActive: (active: boolean) => void;
setActive: (event: EventDocument, active: boolean) => void;
onDelete: (event: EventDocument) => void;
};

Expand All @@ -37,9 +37,11 @@ export const EventCard = (props: EventCardProps) => {
const navigate = useNavigate();
const { t } = useTranslation();

const event = props.event;

const toggleActive = (evt: ChangeEvent<HTMLInputElement>) => {
props.setActive(event, evt.target.checked);
setActive(evt.target.checked);
props.setActive(evt.target.checked);
};

const handleCopy = () => {
Expand Down
15 changes: 8 additions & 7 deletions client/src/components/EventList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,22 @@ const EventList = (props: EventListProps) => {
setEvents(events.filter((ev) => ev._id !== event._id));
};

const updateEventStatus = (event: EventDocument, active: boolean): void => {
console.log("setActive: %o %o", event, active);
updateEvent(event._id, event);
event.isActive = active;
};

const list =
events.length === 0 ? (
<div>{t("sunny_great_halibut_empower")}</div>
) : (
events.map((event, index) => (
events.map((event) => (
<EventCard
key={event._id}
event={event}
url={props.url}
setActive={(active) => {
const event = events[index];
event.isActive = active;
console.log("setActive: %o %o", event, active);
updateEvent(event._id, event);
}}
setActive={updateEventStatus}
onDelete={onDelete}
/>
))
Expand Down

0 comments on commit bd641ff

Please sign in to comment.