Skip to content

Commit

Permalink
Add faircalendar e2e test, fix calendar caching
Browse files Browse the repository at this point in the history
  • Loading branch information
florimondmanca committed Nov 17, 2023
1 parent 86cd179 commit 4edc1ad
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 21 deletions.
26 changes: 26 additions & 0 deletions e2e/faircalendar.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// @ts-check
import { test, expect } from '@playwright/test';

test('auth required', async ({ page }) => {
await page.goto('/app/faircalendar');
await page.waitForURL(url => url.pathname.startsWith('/login'));
});

test.describe.only('authenticated', () => {
test.use({ storageState: 'playwright/.auth/johnDoe.json' });

test('faircalendar content', async ({ page }) => {
await page.goto('/app/faircalendar?year=2023&month=10'); // month=0..11
await expect(page).toHaveTitle('FairCalendar novembre 2023 - Permacoop');

const calendar = page.getByTestId('pc-event-calendar');
await expect(calendar).toBeVisible();

const toussaint = calendar.getByText('7h - Jour férié');
await expect(toussaint).toBeVisible();
});

test('add event', async ({ page }) => {
await page.goto('/app/faircalendar?year=2023&month=10');
});
});
78 changes: 60 additions & 18 deletions src/assets/customElements/eventCalendar.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,54 @@
// @ts-check
import * as Turbo from '@hotwired/turbo';
import Calendar from '@event-calendar/core';
import DayGrid from '@event-calendar/day-grid';
import Interaction from '@event-calendar/interaction';
import { format, subDays } from 'date-fns';

export default class extends HTMLElement {
/** @type {Calendar} */
#ec;

/** @type{string} */
#addUrlTemplate;

connectedCallback() {
this.classList.add('pc-eventcalendar');
this.dataset.testid = 'pc-event-calendar';

const events = JSON.parse(this.dataset.eventsJson);
const events = JSON.parse(this.dataset.eventsJson || '{}');
const addUrlTemplate = this.dataset.addUrlTemplate;
const date = this.dataset.date || new Date('now').toISOString();

if (!addUrlTemplate) {
throw new Error('data-add-url-template is missing');
}

const goToEventCreate = (startDate, endDate) => {
const url = addUrlTemplate
.replace(':startDate', format(startDate, 'yyyy-MM-dd'))
.replace(':endDate', format(endDate, 'yyyy-MM-dd'));
this.#addUrlTemplate = addUrlTemplate;

this.#ec = this.#createCalendar(date, events);

document.addEventListener('turbo:before-cache', this.#onTurboBeforeCache);
}

window.location = url;
};
/**
* @param {Date} startDate
* @param {Date} endDate
*/
#goToEventCreate = (startDate, endDate) => {
const url = this.#addUrlTemplate
.replace(':startDate', format(startDate, 'yyyy-MM-dd'))
.replace(':endDate', format(endDate, 'yyyy-MM-dd'));

const ec = new Calendar({
Turbo.visit(url);
};

/**
* @param {string} date
* @param {any[]} events
* @returns {Calendar}
*/
#createCalendar = (date, events) => {
return new Calendar({
target: this,
props: {
plugins: [DayGrid, Interaction],
Expand All @@ -30,30 +60,42 @@ export default class extends HTMLElement {
6, // Saturday
0 // Sunday
],
date: this.dataset.date,
date,
headerToolbar: { start: '', center: '', end: '' },
dayMaxEvents: true,
eventStartEditable: false,
eventDurationEditable: false,
// TODO: add testid on days
eventContent: ({ event }) => {
const url = event.extendedProps.url;
if (url) {
return { html: `<a href="${url}">${event.title}</a>` };
return {
html: `<a href="${url}" data-turbo-frame="_top">${event.title}</a>`
};
}
return event.title;
},
dateClick: info => goToEventCreate(info.date, info.date),
dateClick: ({ date }) => {
this.#goToEventCreate(date, date);
},
selectable: true,
selectBackgroundColor: 'var(--background-action-violet)',
select: info => {
// By default, range will stay selected if navigating using the
// back button.
ec.unselect();

goToEventCreate(info.start, subDays(info.end, 1));
select: ({ start, end }) => {
this.#goToEventCreate(start, subDays(end, 1));
}
}
}
});
};

#onTurboBeforeCache = () => {
this.#ec.destroy();
};

disconnectedCallback() {
document.removeEventListener(
'turbo:before-cache',
this.#onTurboBeforeCache
);
}
}
4 changes: 2 additions & 2 deletions src/assets/styles/faircalendar.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/* EventCalendar customizations */

.pc-eventcalendar {
pc-eventcalendar {
display: block;
}

Expand All @@ -14,7 +14,7 @@
background-color: var(--event-today);
}

.pc-eventcalendar .ec-day-grid .ec-uniform .ec-day {
pc-eventcalendar .ec-day-grid .ec-uniform .ec-day {
/* Fix: by default EventCalendar sets the min-height of day boxes to 0 when dayMaxEvents is true. */
min-height: 5rem;
}
Expand Down
3 changes: 2 additions & 1 deletion src/templates/pages/faircalendar/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
data-events-json="{{ fullCalendarEvents|dump }}"
data-add-url-template="{{ path('faircalendar_events_add', { startDate: ':startDate', endDate: ':endDate' }) }}"
data-date="{{ date|htmlDate }}"
></pc-eventcalendar>
>
</pc-eventcalendar>
</div>
</div>
</turbo-frame>
Expand Down

0 comments on commit 4edc1ad

Please sign in to comment.