mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
add tests for getShuttlesBySystemId
This commit is contained in:
@@ -5,6 +5,7 @@ import {
|
||||
ApiBasedRepositoryMillisecondTTLs
|
||||
} from "../../src/repositories/ApiBasedRepository";
|
||||
import { IEta } from "../../src/entities/entities";
|
||||
import * as repl from "node:repl";
|
||||
|
||||
/**
|
||||
* Update the global fetch function to return a specific object.
|
||||
@@ -512,10 +513,87 @@ describe("getShuttleById", () => {
|
||||
|
||||
describe("getShuttlesBySystemId", () => {
|
||||
test("getShuttlesBySystemId returns old data if not expired", async () => {
|
||||
updateGlobalFetchMockJson(genericShuttleDataBySystemId);
|
||||
|
||||
const initialCacheShuttle = {
|
||||
coordinates: {
|
||||
latitude: 33.791781,
|
||||
longitude: -117.8589646,
|
||||
},
|
||||
name: "08",
|
||||
routeId: "53966",
|
||||
systemId: "1",
|
||||
id: "5577",
|
||||
millisecondsSinceEpoch: Date.now() - 1000,
|
||||
};
|
||||
|
||||
const initialCache: ApiBasedRepositoryCache = {
|
||||
shuttlesBySystemId: {
|
||||
"1": [
|
||||
initialCacheShuttle,
|
||||
]
|
||||
},
|
||||
shuttleByShuttleId: {
|
||||
"5577": initialCacheShuttle,
|
||||
}
|
||||
};
|
||||
|
||||
const ttls: ApiBasedRepositoryMillisecondTTLs = {
|
||||
shuttleByShuttleId: 100000,
|
||||
shuttlesBySystemId: 100000,
|
||||
};
|
||||
|
||||
const repository = new ApiBasedRepository(initialCache, ttls);
|
||||
const shuttles = await repository.getShuttlesBySystemId("1");
|
||||
expect(shuttles.length).toEqual(1);
|
||||
expect(shuttles[0].id).toEqual(initialCacheShuttle.id);
|
||||
});
|
||||
|
||||
test("getShuttlesBySystemId returns fresh data if expired", async () => {
|
||||
updateGlobalFetchMockJson(genericShuttleDataBySystemId);
|
||||
|
||||
const initialCacheShuttle = {
|
||||
coordinates: {
|
||||
latitude: 33.791781,
|
||||
longitude: -117.8589646,
|
||||
},
|
||||
name: "08",
|
||||
routeId: "53966",
|
||||
systemId: "1",
|
||||
id: "5577",
|
||||
millisecondsSinceEpoch: Date.now() - 100000,
|
||||
};
|
||||
|
||||
const initialCache: ApiBasedRepositoryCache = {
|
||||
shuttlesBySystemId: {
|
||||
"1": [
|
||||
initialCacheShuttle,
|
||||
]
|
||||
},
|
||||
shuttleByShuttleId: {
|
||||
"5577": initialCacheShuttle,
|
||||
}
|
||||
};
|
||||
|
||||
const ttls: ApiBasedRepositoryMillisecondTTLs = {
|
||||
shuttleByShuttleId: 1000,
|
||||
shuttlesBySystemId: 1000,
|
||||
};
|
||||
|
||||
const repository = new ApiBasedRepository(initialCache, ttls);
|
||||
const shuttles = await repository.getShuttlesBySystemId("1");
|
||||
|
||||
expect(shuttles.length).toEqual(1);
|
||||
expect(shuttles[0].id).toEqual("5577");
|
||||
expect(shuttles[0].millisecondsSinceEpoch).not.toEqual(initialCacheShuttle.millisecondsSinceEpoch);
|
||||
});
|
||||
})
|
||||
|
||||
test("getShuttlesBySystemId returns fresh data if no seeded data", async () => {
|
||||
updateGlobalFetchMockJson(genericShuttleDataBySystemId);
|
||||
|
||||
const repository = new ApiBasedRepository();
|
||||
const shuttles = await repository.getShuttlesBySystemId("1");
|
||||
|
||||
expect(shuttles.length).toEqual(1);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user