Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/models/TerritoryControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ const TerritoryControlSchema = new mongoose.Schema({
required: [true, 'Territory control date is required'],
validate: {
validator: function(value) {
return value <= new Date();
const today = new Date();
today.setHours(23, 59, 59, 999); // Set to end of current day
return value <= today;
Comment on lines +141 to +143

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The variable today is a bit misleading as it's immediately mutated to represent the end of the current day. Using a more descriptive variable name would improve code clarity and make the validator's logic easier to understand at a glance.

Suggested change
const today = new Date();
today.setHours(23, 59, 59, 999); // Set to end of current day
return value <= today;
const endOfToday = new Date();
endOfToday.setHours(23, 59, 59, 999); // Set to end of current day
return value <= endOfToday;

},
message: 'Territory control date cannot be in the future'
}
Expand Down
38 changes: 38 additions & 0 deletions src/tests/models/territoryControl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,44 @@ describe('TerritoryControl Model', () => {

await expect(territoryControl.save()).rejects.toThrow();
});

it('should allow same-day dates', async () => {
const today = new Date();
today.setHours(0, 0, 0, 0); // Set to start of today

const territoryControlData = {
type: 'FeatureCollection',
date: today,
features: [
{
type: 'Feature',
properties: {
name: 'Test Territory Today',
controlledBy: 'sdf',
color: '#ffff00',
controlledSince: '2020-01-01'
},
geometry: {
type: 'Polygon',
coordinates: [
[
[35.0, 36.0],
[36.0, 36.0],
[36.0, 37.0],
[35.0, 37.0],
[35.0, 36.0]
]
]
}
}
]
};

const territoryControl = new TerritoryControl(territoryControlData);

// This should not throw an error
await expect(territoryControl.save()).resolves.toBeDefined();
});
Comment on lines +297 to +333

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This test relies on the actual system time via new Date(), which can make it "flaky". A flaky test is one that can pass or fail without any code changes, for example if it's run at a different time of day or in a different timezone. This can disrupt development workflows and hide real bugs.

To make this test deterministic and reliable, it's best practice to mock the system clock using Jest's fake timers. This ensures new Date() returns a predictable value, making the test's outcome consistent regardless of when or where it is run.

    it('should allow same-day dates', async () => {
      // Mock the system time to ensure the test is deterministic and not affected by the actual time it's run.
      const mockDate = new Date('2024-05-20T12:00:00.000Z');
      jest.useFakeTimers().setSystemTime(mockDate);

      const territoryControlData = {
        type: 'FeatureCollection',
        date: new Date(), // This will use the mocked date, which is valid.
        features: [
          {
            type: 'Feature',
            properties: {
              name: 'Test Territory Today',
              controlledBy: 'sdf',
              color: '#ffff00',
              controlledSince: '2020-01-01'
            },
            geometry: {
              type: 'Polygon',
              coordinates: [
                [
                  [35.0, 36.0],
                  [36.0, 36.0],
                  [36.0, 37.0],
                  [35.0, 37.0],
                  [35.0, 36.0]
                ]
              ]
            }
          }
        ]
      };

      const territoryControl = new TerritoryControl(territoryControlData);
      
      // This should not throw an error because the mocked date is not in the future.
      await expect(territoryControl.save()).resolves.toBeDefined();

      // It's important to restore the real timers after the test.
      jest.useRealTimers();
    });

});

describe('Static Methods', () => {
Expand Down
Loading