Skip to content

Commit

Permalink
feat: unit test cases for isStoredItineraryRelevant
Browse files Browse the repository at this point in the history
  • Loading branch information
partisaani committed Nov 13, 2024
1 parent b8b5f1c commit 1ad32a8
Showing 1 changed file with 143 additions and 0 deletions.
143 changes: 143 additions & 0 deletions test/unit/views/ItineraryPage/ItineraryPageUtils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import { isStoredItineraryRelevant } from '../../../../app/component/itinerary/ItineraryPageUtils';

describe('itineraryPageUtils', () => {
describe('isStoredItineraryRelevant', () => {
const TIME = 12345;
const ARRIVE_BY = false;
const INDEX = '0';
const TEN_MINUTES_FROM_NOW = new Date(Date.now() + 600000).toISOString();

let mockStoredItinerary;
let mockMatch;

beforeEach(() => {
mockStoredItinerary = {
itinerary: {
end: TEN_MINUTES_FROM_NOW,
},
params: {
from: 'foo',
to: 'bar',
arriveBy: ARRIVE_BY,
time: TIME,
hash: INDEX,
secondHash: undefined,
},
};

mockMatch = {
params: { from: 'foo', to: 'bar', hash: INDEX, secondHash: undefined },
location: { query: { time: TIME, arriveBy: ARRIVE_BY } },
};
});

it('should return true for equal params', () => {
expect(
isStoredItineraryRelevant(mockStoredItinerary, mockMatch),
).to.equal(true);
});

it('should return true if stored index matches secondHash', () => {
expect(
isStoredItineraryRelevant(mockStoredItinerary, {
...mockMatch,
hash: 'other',
secondHash: INDEX,
}),
).to.equal(true);
});

it('should return true if arriveBy is undefined for both', () => {
const itineraryWithoutArriveBy = { ...mockStoredItinerary };
const matchWithoutArriveBy = { ...mockMatch };
itineraryWithoutArriveBy.params.arriveBy = undefined;
matchWithoutArriveBy.location.query.arriveBy = undefined;

expect(
isStoredItineraryRelevant(
itineraryWithoutArriveBy,
matchWithoutArriveBy,
),
).to.equal(true);
});

it('should return false for past itinerary', () => {
const TEN_MINUTES_IN_THE_PAST = new Date(
Date.now() + 600000,
).toISOString();

expect(
isStoredItineraryRelevant(
{
...mockStoredItinerary,
itinerary: { end: TEN_MINUTES_IN_THE_PAST },
},
mockMatch,
),
).to.equal(true);
});

it('should return false on index and hash mismatch if secondHash is undefined', () => {
const matchWithDifferentHash = { ...mockMatch };
matchWithDifferentHash.params.hash = '999';
matchWithDifferentHash.params.secondHash = undefined;

expect(
isStoredItineraryRelevant(mockStoredItinerary, matchWithDifferentHash),
).to.equal(false);
});

it('should return false on index and hash mismatch if hash and secondHash are undefined', () => {
const matchWithDifferentHash = { ...mockMatch };
matchWithDifferentHash.params.hash = undefined;
matchWithDifferentHash.params.secondHash = undefined;

expect(
isStoredItineraryRelevant(mockStoredItinerary, matchWithDifferentHash),
).to.equal(false);
});

it('should throw error if match is undefined', () => {
expect(() =>
isStoredItineraryRelevant(mockStoredItinerary, undefined),
).to.throw(Error);
});

it('should not throw error if stored itinerary is empty object', () => {
expect(() => isStoredItineraryRelevant({}, mockMatch)).to.not.throw(
Error,
);
expect(isStoredItineraryRelevant({}, mockMatch)).to.equal(false);
});

it('should not throw error if stored itinerary is missing itinerary field', () => {
expect(() =>
isStoredItineraryRelevant(
{ ...mockStoredItinerary, itinerary: undefined },
mockMatch,
),
).to.not.throw(Error);
expect(
isStoredItineraryRelevant(
{ ...mockStoredItinerary, itinerary: undefined },
mockMatch,
),
).to.equal(false);
});

it('should not throw error if stored itinerary is missing itinerary field', () => {
expect(() =>
isStoredItineraryRelevant(
{ ...mockStoredItinerary, params: undefined },
mockMatch,
),
).to.not.throw(Error);
expect(
isStoredItineraryRelevant(
{ ...mockStoredItinerary, params: undefined },
mockMatch,
),
).to.equal(false);
});
});
});

0 comments on commit 1ad32a8

Please sign in to comment.