Skip to content

Commit

Permalink
Merge 65619a2 into de21b1f
Browse files Browse the repository at this point in the history
  • Loading branch information
Qwebeck authored Jan 9, 2021
2 parents de21b1f + 65619a2 commit bb47d67
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 53 deletions.
Binary file added cypress/fixtures/example_2.xlsx
Binary file not shown.
Binary file modified cypress/fixtures/grafik.xlsx
Binary file not shown.
37 changes: 0 additions & 37 deletions cypress/integration/e2e/table/load-exported-schedule.spec.ts

This file was deleted.

75 changes: 75 additions & 0 deletions cypress/integration/e2e/table/load-schedule.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

import { ShiftCode } from "../../../../src/common-models/shift-info.model";
import { WorkerType } from "../../../../src/common-models/worker-info.model";

context("Load schedule", () => {
it("Shoud be able to load schedule after clicking save with empty state to database", () => {
cy.visit(Cypress.env("baseUrl"));
cy.get("[data-cy=nurseShiftsTable]", { timeout: 10000 }).should("not.exist");
cy.get("[data-cy=edit-mode-button]").click();
cy.saveToDatabase();
cy.get("[data-cy=leave-edit-mode]").click();
cy.loadSchedule("example_2.xlsx");
cy.checkWorkerShift({
workerType: WorkerType.NURSE,
workerIdx: 0,
shiftIdx: 0,
desiredShiftCode: ShiftCode.N,
});
});

it("Shoud be able to save file to database and after that load new schedule", () => {
const cell = {
workerType: WorkerType.NURSE,
workerIdx: 0,
shiftIdx: 0,
};
cy.loadSchedule("example.xlsx");
cy.checkWorkerShift({
...cell,
desiredShiftCode: ShiftCode.DN,
});
cy.enterEditMode();
cy.saveToDatabase();
cy.leaveEditMode();
cy.loadSchedule("example_2.xlsx");
cy.checkWorkerShift({
...cell,
desiredShiftCode: ShiftCode.N,
});
});

it("Should be able to save file and load the exported file", () => {
cy.loadSchedule();

cy.get("[data-cy=export-schedule-button]").click();

cy.get("a[download]")
.then(
(anchor) =>
new Cypress.Promise((resolve) => {
const xhr = new XMLHttpRequest();
xhr.open("GET", anchor.prop("href"), true);
xhr.responseType = "blob";
xhr.onload = (): void => {
if (xhr.status === 200) {
const blob = xhr.response;
const reader = new FileReader();
reader.onload = (): void => {
resolve(reader.result);
};
reader.readAsBinaryString(blob);
}
};
xhr.send();
})
)
.then((file: string) => {
cy.writeFile("cypress/fixtures/grafik.xlsx", file, "binary");
cy.get('[data-cy="file-input"]').attachFile("grafik.xlsx");
});
});
});
18 changes: 13 additions & 5 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import "cypress-file-upload";
import { WorkerType } from "../../src/common-models/worker-info.model";
import { ShiftCode } from "../../src/common-models/shift-info.model";

import { WorkerType } from "../../src/common-models/worker-info.model";
export interface GetWorkerShiftOptions {
workerType: WorkerType;
workerIdx: number;
Expand All @@ -17,7 +16,6 @@ export interface CheckWorkerShiftOptions extends GetWorkerShiftOptions {
export interface ChangeWorkerShiftOptions extends GetWorkerShiftOptions {
newShiftCode: ShiftCode;
}

export interface CheckHoursInfoOptions {
workerType: WorkerType;
workerIdx: number;
Expand All @@ -33,11 +31,12 @@ export enum HoursInfoCells {
actual = 1,
overtime = 2,
}
export type ScheduleName = "example.xlsx" | "grafik.xlsx" | "example_2.xlsx";

Cypress.Commands.add("loadSchedule", () => {
Cypress.Commands.add("loadSchedule", (scheduleName: ScheduleName = "example.xlsx") => {
cy.visit(Cypress.env("baseUrl"));
cy.get("[data-cy=file-dropdown]").click();
cy.get('[data-cy="file-input"]').attachFile("example.xlsx");
cy.get('[data-cy="file-input"]').attachFile(scheduleName);
cy.get(`[data-cy=nurseShiftsTable]`, { timeout: 10000 });
cy.window()
.its("store")
Expand Down Expand Up @@ -104,7 +103,16 @@ Cypress.Commands.add(
}
);

Cypress.Commands.add("saveToDatabase", () => {
return cy.get("[data-cy=save-schedule-button").click();
});

Cypress.Commands.add("enterEditMode", () => {
cy.get("[data-cy=edit-mode-button]").click();
return cy.get("[data-cy=nurseShiftsTable]", { timeout: 10000 });
});

Cypress.Commands.add("leaveEditMode", () => {
cy.get("[data-cy=leave-edit-mode]").click();
return cy.get("[data-cy=nurseShiftsTable]", { timeout: 10000 });
});
4 changes: 3 additions & 1 deletion cypress/support/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
declare namespace Cypress {
interface Chainable {
loadSchedule(): Chainable<Element>;
loadSchedule(scheduleName?: import("./commands").ScheduleName): Chainable<Element>;

useAutocomplete(
newShiftCode: import("../../src/common-models/shift-info.model").ShiftCode
Expand All @@ -26,5 +26,7 @@ declare namespace Cypress {
): Chainable<Element>;

enterEditMode(): Chainable<Element>;
saveToDatabase(): Chainable<Element>;
leaveEditMode(): Chainable<Element>;
}
}
8 changes: 6 additions & 2 deletions src/api/local-storage-provider.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import PouchDB from "pouchdb-browser";
import { ScheduleDataModel } from "../common-models/schedule-data.model";
import { isScheduleModelEmpty, ScheduleDataModel } from "../common-models/schedule-data.model";
import { ScheduleDataActionCreator } from "../state/reducers/month-state/schedule-data/schedule-data.action-creator";
import {
PersistanceStoreProvider,
Expand All @@ -12,20 +12,24 @@ import {
ThunkFunction,
} from "./persistance-store.model";

export const DATABASE_NAME = "nurse-scheduling";
/*eslint-disable @typescript-eslint/camelcase */
export class LocalStorageProvider extends PersistanceStoreProvider {
private storage: PouchDB.Database<ScheduleRevision>;

constructor() {
super();
this.storage = new PouchDB("nurse-scheduling");
this.storage = new PouchDB(DATABASE_NAME);
}

saveScheduleRevision(
type: RevisionType,
schedule: ScheduleDataModel
): ThunkFunction<ScheduleDataModel> {
return async (dispatch): Promise<void> => {
if (isScheduleModelEmpty(schedule)) {
return;
}
const id = this.getScheduleId(schedule);
let revision = "";
try {
Expand Down
10 changes: 9 additions & 1 deletion src/common-models/schedule-data.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@ import { WorkersInfoModel } from "./worker-info.model";
import { MonthInfoModel } from "./month-info.model";
import { ScheduleModel } from "./schedule.model";
import { ShiftInfoModel } from "./shift-info.model";
import _ from "lodash";

export interface ScheduleDataModel {
schedule_info: ScheduleModel;
month_info: MonthInfoModel;
employee_info: WorkersInfoModel;
shifts: ShiftInfoModel;
isNew?: boolean;
}

export function isScheduleModelEmpty(scheduleModel: ScheduleDataModel): boolean {
const requiredFields: (keyof ScheduleDataModel)[] = ["employee_info", "month_info", "shifts"];
return requiredFields.every((field) => {
const requiredObject = scheduleModel[field];
return Object.values(requiredObject).every((field) => _.isEmpty(field));
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,13 @@ export function EditPageToolbar({ closeEdit }: EditPageToolbarOptions): JSX.Elem
</Button>

<Link to="/">
<Button onClick={closeEdit} size="small" className="submit-button" variant="primary">
<Button
onClick={closeEdit}
size="small"
className="submit-button"
variant="primary"
data-cy="leave-edit-mode"
>
Wyjdź
</Button>
</Link>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
import React, { useEffect } from "react";
import { useSelector } from "react-redux";
import { ApplicationStateModel } from "../../../state/models/application-state.model";
import { PERSISTENT_SCHEDULE_UNDOABLE_CONFIG } from "../../../state/reducers/month-state/schedule-data/schedule.actions";
import { UndoableHotkeys } from "../../common-components";
import { ScheduleComponent } from "../table/schedule/schedule.component";
import { ScheduleLogicContext, useScheduleState } from "../table/schedule/use-schedule-state";
import { ViewOnlyToolbar } from "./view-only-toolbar";
import { UndoableHotkeys } from "../../common-components";
import { PERSISTENT_SCHEDULE_UNDOABLE_CONFIG } from "../../../state/reducers/month-state/schedule-data/schedule.actions";

interface ScheduleViewOnlyPageOptions {
openEdit: () => void;
}

export function ScheduleViewOnlyPage(props: ScheduleViewOnlyPageOptions): JSX.Element {
const scheduleModel = useSelector((state: ApplicationStateModel) => {
return state.actualState.persistentSchedule.present;
});
const scheduleModel = useSelector(
(state: ApplicationStateModel) => state.actualState.persistentSchedule.present
);

const { scheduleLogic, setNewSchedule, scheduleLocalState } = useScheduleState(
scheduleModel,
"readonly"
Expand Down
3 changes: 3 additions & 0 deletions src/state/reducers/undoable.action-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export interface UndoableConfig<T> {
export class UndoActionCreator {
static undo({ undoType, afterUndo }: UndoableConfig<unknown>): ThunkFunction<unknown> {
return async (dispatch, getState): Promise<void> => {
if (getState().actualState.temporarySchedule.past.length === 1) {
return;
}
dispatch({
type: undoType,
});
Expand Down

0 comments on commit bb47d67

Please sign in to comment.