Skip to content

Commit

Permalink
[MegaLinter] Apply linters fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wesley-dean-gsa committed Nov 21, 2024
1 parent b24610f commit c86f957
Show file tree
Hide file tree
Showing 9 changed files with 4,945 additions and 133 deletions.
2 changes: 1 addition & 1 deletion .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const {
getStateFromDates,
htmlDateString,
minNumber,
uswdsIcon
uswdsIcon,
} = require("./js/global.js");

require("dotenv").config();
Expand Down
2 changes: 1 addition & 1 deletion _data/assetPaths.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
"subnav.map": "/assets/js/subnav-3QHQ2EX4.js.map",
"styles.css": "/assets/styles/styles-WFQMHBOR.css",
"styles.map": "/assets/styles/styles-WFQMHBOR.css.map"
}
}
46 changes: 23 additions & 23 deletions _tests/getStateFromDates.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
const { getStateFromDates } = require('../js/global');
const { getStateFromDates } = require("../js/global");

describe('getStateFromDates', () => {
describe("getStateFromDates", () => {
beforeEach(() => {
// Mock the system time to ensure consistent results
jest.useFakeTimers().setSystemTime(new Date('2024-11-21T12:00:00Z')); // Mock current time: 7 AM ET
jest.useFakeTimers().setSystemTime(new Date("2024-11-21T12:00:00Z")); // Mock current time: 7 AM ET
});

afterEach(() => {
jest.useRealTimers(); // Restore real timers
});

test('should return "unknown" if both opens and closes are undefined', () => {
expect(getStateFromDates(null, null)).toBe('unknown');
expect(getStateFromDates(undefined, undefined)).toBe('unknown');
expect(getStateFromDates(null, null)).toBe("unknown");
expect(getStateFromDates(undefined, undefined)).toBe("unknown");
});

test('should return "upcoming" if now is before opens', () => {
const opens = '2024-11-22T00:00:00Z'; // Opens tomorrow at midnight UTC
expect(getStateFromDates(opens, null)).toBe('upcoming');
const opens = "2024-11-22T00:00:00Z"; // Opens tomorrow at midnight UTC
expect(getStateFromDates(opens, null)).toBe("upcoming");
});

test('should return "open" if now is after opens and before closes', () => {
const opens = '2024-11-20T00:00:00Z'; // Opened yesterday at midnight UTC
const closes = '2024-11-22T00:00:00Z'; // Closes tomorrow at midnight UTC
expect(getStateFromDates(opens, closes)).toBe('open');
const opens = "2024-11-20T00:00:00Z"; // Opened yesterday at midnight UTC
const closes = "2024-11-22T00:00:00Z"; // Closes tomorrow at midnight UTC
expect(getStateFromDates(opens, closes)).toBe("open");
});

test('should return "closed" if now is after closes', () => {
const opens = '2024-11-19T00:00:00Z'; // Opened two days ago at midnight UTC
const closes = '2024-11-20T23:59:59Z'; // Closed yesterday at 11:59:59 PM UTC
expect(getStateFromDates(opens, closes)).toBe('closed');
const opens = "2024-11-19T00:00:00Z"; // Opened two days ago at midnight UTC
const closes = "2024-11-20T23:59:59Z"; // Closed yesterday at 11:59:59 PM UTC
expect(getStateFromDates(opens, closes)).toBe("closed");
});

test('should handle cases with only opens defined', () => {
const opens = '2024-11-20T00:00:00Z'; // Opened yesterday at midnight UTC
expect(getStateFromDates(opens, null)).toBe('open');
test("should handle cases with only opens defined", () => {
const opens = "2024-11-20T00:00:00Z"; // Opened yesterday at midnight UTC
expect(getStateFromDates(opens, null)).toBe("open");
});

test('should handle cases with only closes defined', () => {
const closes = '2024-11-22T00:00:00Z'; // Closes tomorrow at midnight UTC
expect(getStateFromDates(null, closes)).toBe('unknown'); // No opens means "unknown"
test("should handle cases with only closes defined", () => {
const closes = "2024-11-22T00:00:00Z"; // Closes tomorrow at midnight UTC
expect(getStateFromDates(null, closes)).toBe("unknown"); // No opens means "unknown"
});

test('should handle edge cases for opens and closes on the same day', () => {
const opens = '2024-11-21T00:00:00Z'; // Opens today at midnight UTC
const closes = '2024-11-21T23:59:59Z'; // Closes today at 11:59:59 PM UTC
expect(getStateFromDates(opens, closes)).toBe('open'); // Current time is 7 AM ET
test("should handle edge cases for opens and closes on the same day", () => {
const opens = "2024-11-21T00:00:00Z"; // Opens today at midnight UTC
const closes = "2024-11-21T23:59:59Z"; // Closes today at 11:59:59 PM UTC
expect(getStateFromDates(opens, closes)).toBe("open"); // Current time is 7 AM ET
});
});
16 changes: 8 additions & 8 deletions _tests/minNumber.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
const { minNumber } = require('../js/global');
const { minNumber } = require("../js/global");

describe('minNumber', () => {
test('should return the smallest number from a list of numbers', () => {
describe("minNumber", () => {
test("should return the smallest number from a list of numbers", () => {
const result = minNumber(5, 10, 3, 8, 2);
expect(result).toBe(2);
});

test('should return the only number when a single number is provided', () => {
test("should return the only number when a single number is provided", () => {
const result = minNumber(7);
expect(result).toBe(7);
});

test('should handle negative numbers correctly', () => {
test("should handle negative numbers correctly", () => {
const result = minNumber(-10, -5, -30, 0);
expect(result).toBe(-30);
});

test('should handle a mix of positive and negative numbers', () => {
test("should handle a mix of positive and negative numbers", () => {
const result = minNumber(15, -20, 35, 0, -5);
expect(result).toBe(-20);
});

test('should return NaN if any of the inputs are not numbers', () => {
const result = minNumber(5, 'a', 10, {}, []);
test("should return NaN if any of the inputs are not numbers", () => {
const result = minNumber(5, "a", 10, {}, []);
expect(result).toBeNaN();
});
});
34 changes: 20 additions & 14 deletions _tests/readableDate.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
const { DateTime } = require('luxon');
const { readableDate } = require('../js/global');
const { DateTime } = require("luxon");
const { readableDate } = require("../js/global");

describe('readableDate', () => {
describe("readableDate", () => {
test('should return the formatted date in "dd LLL yyyy" format for valid dates', () => {
const date = new Date('2024-11-21T15:30:00Z'); // Example date
const expected = DateTime.fromJSDate(date, { zone: 'America/New_York' }).toFormat('dd LLL yyyy');
const date = new Date("2024-11-21T15:30:00Z"); // Example date
const expected = DateTime.fromJSDate(date, {
zone: "America/New_York",
}).toFormat("dd LLL yyyy");
expect(readableDate(date)).toBe(expected);
});

test('should handle different time zones and return consistent output', () => {
const date = new Date('2024-07-04T12:00:00Z');
const expected = DateTime.fromJSDate(date, { zone: 'America/New_York' }).toFormat('dd LLL yyyy');
test("should handle different time zones and return consistent output", () => {
const date = new Date("2024-07-04T12:00:00Z");
const expected = DateTime.fromJSDate(date, {
zone: "America/New_York",
}).toFormat("dd LLL yyyy");
expect(readableDate(date)).toBe(expected);
});

test('should throw an error or handle gracefully when input is not a valid date', () => {
const invalidInputs = [null, undefined, 'invalid date', {}, [], 12345];
test("should throw an error or handle gracefully when input is not a valid date", () => {
const invalidInputs = [null, undefined, "invalid date", {}, [], 12345];
invalidInputs.forEach((input) => {
expect(() => readableDate(input)).toThrow(); // Adjust this if your function handles invalid input differently
});
});

test('should handle edge case dates correctly', () => {
test("should handle edge case dates correctly", () => {
const edgeDates = [
new Date('1970-01-01T00:00:00Z'), // Unix epoch start
new Date('9999-12-31T23:59:59Z'), // Far future date
new Date("1970-01-01T00:00:00Z"), // Unix epoch start
new Date("9999-12-31T23:59:59Z"), // Far future date
];

edgeDates.forEach((date) => {
const expected = DateTime.fromJSDate(date, { zone: 'America/New_York' }).toFormat('dd LLL yyyy');
const expected = DateTime.fromJSDate(date, {
zone: "America/New_York",
}).toFormat("dd LLL yyyy");
expect(readableDate(date)).toBe(expected);
});
});
Expand Down
108 changes: 54 additions & 54 deletions _tests/sortByProp.js
Original file line number Diff line number Diff line change
@@ -1,112 +1,112 @@
const { sortByProp } = require("../js/global");

describe('sortByProp', () => {
test('should sort an array of objects by a numeric property (Data Analyst)', () => {
describe("sortByProp", () => {
test("should sort an array of objects by a numeric property (Data Analyst)", () => {
const input = [
{ id: 3, name: 'Data Analyst' },
{ id: 1, name: 'Content Manager' },
{ id: 2, name: 'Web Developer' },
{ id: 3, name: "Data Analyst" },
{ id: 1, name: "Content Manager" },
{ id: 2, name: "Web Developer" },
];

const result = sortByProp(input, 'id');
const result = sortByProp(input, "id");
expect(result).toEqual([
{ id: 1, name: 'Content Manager' },
{ id: 2, name: 'Web Developer' },
{ id: 3, name: 'Data Analyst' },
{ id: 1, name: "Content Manager" },
{ id: 2, name: "Web Developer" },
{ id: 3, name: "Data Analyst" },
]);
});

test('should sort an array of objects by a string property alphabetically (Content Manager)', () => {
test("should sort an array of objects by a string property alphabetically (Content Manager)", () => {
const input = [
{ id: 1, name: 'Data Analyst' },
{ id: 2, name: 'Content Manager' },
{ id: 3, name: 'Web Developer' },
{ id: 1, name: "Data Analyst" },
{ id: 2, name: "Content Manager" },
{ id: 3, name: "Web Developer" },
];

const result = sortByProp(input, 'name');
const result = sortByProp(input, "name");
expect(result).toEqual([
{ id: 2, name: 'Content Manager' },
{ id: 1, name: 'Data Analyst' },
{ id: 3, name: 'Web Developer' },
{ id: 2, name: "Content Manager" },
{ id: 1, name: "Data Analyst" },
{ id: 3, name: "Web Developer" },
]);
});

test('should handle mixed data types (Web Developer)', () => {
test("should handle mixed data types (Web Developer)", () => {
const input = [
{ id: 3, name: 'Web Developer' },
{ id: 2, name: 'Data Analyst' },
{ id: 1, name: 'Content Manager' },
{ id: 3, name: "Web Developer" },
{ id: 2, name: "Data Analyst" },
{ id: 1, name: "Content Manager" },
];

const result = sortByProp(input, 'id');
const result = sortByProp(input, "id");
expect(result).toEqual([
{ id: 1, name: 'Content Manager' },
{ id: 2, name: 'Data Analyst' },
{ id: 3, name: 'Web Developer' },
{ id: 1, name: "Content Manager" },
{ id: 2, name: "Data Analyst" },
{ id: 3, name: "Web Developer" },
]);
});

test('should handle an empty array', () => {
test("should handle an empty array", () => {
const input = [];
const result = sortByProp(input, 'id');
const result = sortByProp(input, "id");
expect(result).toEqual([]);
});

test('should return a new array without modifying the original array', () => {
test("should return a new array without modifying the original array", () => {
const input = [
{ id: 2, name: 'Web Developer' },
{ id: 1, name: 'Content Manager' },
{ id: 2, name: "Web Developer" },
{ id: 1, name: "Content Manager" },
];

const result = sortByProp(input, 'id');
const result = sortByProp(input, "id");
expect(result).toEqual([
{ id: 1, name: 'Content Manager' },
{ id: 2, name: 'Web Developer' },
{ id: 1, name: "Content Manager" },
{ id: 2, name: "Web Developer" },
]);

expect(input).toEqual([
{ id: 2, name: 'Web Developer' },
{ id: 1, name: 'Content Manager' },
{ id: 2, name: "Web Developer" },
{ id: 1, name: "Content Manager" },
]); // Ensure original array is unchanged
});

test('should handle properties that do not exist on all objects', () => {
test("should handle properties that do not exist on all objects", () => {
const input = [
{ id: 3, name: 'Data Analyst' },
{ id: 3, name: "Data Analyst" },
{ id: 1 },
{ id: 2, name: 'Web Developer' },
{ id: 2, name: "Web Developer" },
];

const result = sortByProp(input, 'name');
const result = sortByProp(input, "name");
expect(result).toEqual([
{ id: 3, name: 'Data Analyst' },
{ id: 2, name: 'Web Developer' },
{ id: 3, name: "Data Analyst" },
{ id: 2, name: "Web Developer" },
{ id: 1 }, // Objects without the property should stay at their original position
]);
});

test('should handle an array with non-object elements gracefully', () => {
test("should handle an array with non-object elements gracefully", () => {
const input = [
{ id: 1, name: 'Content Manager' },
'randomString',
{ id: 2, name: 'Web Developer' },
{ id: 1, name: "Content Manager" },
"randomString",
{ id: 2, name: "Web Developer" },
];

expect(() => sortByProp(input, 'id')).toThrow();
expect(() => sortByProp(input, "id")).toThrow();
});

test('should handle sorting with numeric strings correctly', () => {
test("should handle sorting with numeric strings correctly", () => {
const input = [
{ id: '3', name: 'Web Developer' },
{ id: '2', name: 'Data Analyst' },
{ id: '1', name: 'Content Manager' },
{ id: "3", name: "Web Developer" },
{ id: "2", name: "Data Analyst" },
{ id: "1", name: "Content Manager" },
];

const result = sortByProp(input, 'id');
const result = sortByProp(input, "id");
expect(result).toEqual([
{ id: '1', name: 'Content Manager' },
{ id: '2', name: 'Data Analyst' },
{ id: '3', name: 'Web Developer' },
{ id: "1", name: "Content Manager" },
{ id: "2", name: "Data Analyst" },
{ id: "3", name: "Web Developer" },
]);
});
});
Loading

0 comments on commit c86f957

Please sign in to comment.