Skip to content

Commit

Permalink
Merge pull request #100 from mbta/pwd-schedule_finder_fetches_schedul…
Browse files Browse the repository at this point in the history
…es_async

Pwd schedule finder fetches schedules async
  • Loading branch information
phildarnowsky authored Jul 18, 2019
2 parents 3b96c34 + 7afd5f8 commit b627736
Show file tree
Hide file tree
Showing 10 changed files with 257 additions and 5,793 deletions.
48 changes: 48 additions & 0 deletions apps/site/assets/css/_schedule-page.scss
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,51 @@
justify-content: space-evenly;
padding: $base-spacing / 2;
}

.schedule-finder__spinner,
.schedule-finder__spinner::before,
.schedule-finder__spinner::after {
animation: load7 1.8s infinite ease-in-out;
animation-fill-mode: both;
border-radius: 50%;
height: 2.5em;
width: 2.5em;
}

.schedule-finder__spinner {
animation-delay: -.16s;
color: $brand-primary;
font-size: 10px;
margin: 80px auto;
position: relative;
text-indent: -9999em;
transform: translateZ(0);
}

.schedule-finder__spinner::before,
.schedule-finder__spinner::after {
content: '';
position: absolute;
top: 0;
}

.schedule-finder__spinner::before {
animation-delay: -.32s;
left: -3.5em;
}

.schedule-finder__spinner::after {
left: 3.5em;
}

@keyframes load7 {
0%,
80%,
100% {
box-shadow: 0 2.5em 0 -1.3em;
}

40% {
box-shadow: 0 2.5em 0 0;
}
}
3 changes: 0 additions & 3 deletions apps/site/assets/ts/schedule/__tests__/ScheduleModalTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ describe("ScheduleModal", () => {
selectedOrigin={stops[0].id}
selectedDirection={0}
services={[]}
serviceSchedules={{}}
/>
);
});
Expand All @@ -105,7 +104,6 @@ describe("ScheduleModal", () => {
selectedOrigin={null}
selectedDirection={0}
services={[]}
serviceSchedules={{}}
/>
);
expect(tree!.toJSON()).toBeNull();
Expand All @@ -122,7 +120,6 @@ describe("ScheduleModal", () => {
selectedOrigin={stops[0].id}
selectedDirection={null}
services={[]}
serviceSchedules={{}}
/>
);
expect(tree!.toJSON()).toBeNull();
Expand Down
103 changes: 97 additions & 6 deletions apps/site/assets/ts/schedule/__tests__/ServiceSelectorTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import React from "react";
import renderer from "react-test-renderer";
import { createReactRoot } from "../../app/helpers/testUtils";
import serviceData from "./serviceData.json";
import ServiceSelector from "../components/schedule-finder/ServiceSelector";
import {
fetchSchedule,
ServiceSelector
} from "../components/schedule-finder/ServiceSelector";
import { ServiceSchedule } from "../components/__schedule.js";
import { ServiceWithServiceDate } from "../../__v3api";

Expand Down Expand Up @@ -72,12 +75,100 @@ describe("ServiceSelector", () => {
it("it renders", () => {
createReactRoot();
const tree = renderer.create(
<ServiceSelector
services={services}
directionId={0}
serviceSchedules={serviceSchedules}
/>
<ServiceSelector services={services} directionId={0} routeId={"111"} />
);
expect(tree).toMatchSnapshot();
});

describe("fetchSchedule", () => {
it("fetches the selected schedule", async () => {
window.fetch = jest.fn().mockImplementation(
() =>
new Promise((resolve: Function) =>
resolve({
json: () => {
return {
by_trip: "by_trip_data",
trip_order: "trip_order_data"
};
},
ok: true,
status: 200,
statusText: "OK"
})
)
);

var loadingSpy = jest.fn();
var serviceScheduleSpy = jest.fn();

await await fetchSchedule(
services,
"BUS319-P-Sa-02",
"83",
1,
loadingSpy,
serviceScheduleSpy
);

expect(window.fetch).toHaveBeenCalledWith(
"/schedules/schedule_api?id=83&date=2019-08-31&direction_id=1"
);

expect(loadingSpy).toHaveBeenCalledTimes(2);
expect(loadingSpy).toHaveBeenCalledWith(true);
expect(loadingSpy).toHaveBeenLastCalledWith(false);

expect(serviceScheduleSpy).toHaveBeenCalledTimes(1);
expect(serviceScheduleSpy).toHaveBeenCalledWith({
by_trip: "by_trip_data",
trip_order: "trip_order_data"
});
}),
it("fails quietly if called with an invalid service ID", () => {
window.fetch = jest.fn();
var loadingSpy = jest.fn();
var serviceScheduleSpy = jest.fn();

fetchSchedule(
services,
"BAD-SERVICE-ID",
"83",
1,
loadingSpy,
serviceScheduleSpy
);
expect(window.fetch).not.toHaveBeenCalled();
}),
it("throws an error if the fetch fails", async () => {
window.fetch = jest.fn().mockImplementation(
() =>
new Promise((resolve: Function) =>
resolve({
ok: false,
status: 500,
statusText: "you broke it"
})
)
);

var loadingSpy = jest.fn();
var serviceScheduleSpy = jest.fn();

await fetchSchedule(
services,
"BUS319-P-Sa-02",
"83",
1,
loadingSpy,
serviceScheduleSpy
);

expect(loadingSpy).toHaveBeenCalledTimes(2);
expect(loadingSpy).toHaveBeenCalledWith(true);
expect(loadingSpy).toHaveBeenLastCalledWith(false);

expect(serviceScheduleSpy).not.toHaveBeenCalled();
});
});
});
Loading

0 comments on commit b627736

Please sign in to comment.