Documentation

File: Helper.unit.test.js

Description: This file contains unit tests for the Helpers class within the @/utils directory. The tests cover various helper functions designed for general utility purposes.

Class: Utils

Description: This is a test suite, not a class, designed to test utility functions within the Helpers class.

Methods


Method: isSupported

Description: This test suite verifies the functionality of the isSupported method from the Helpers class.

Parameters: N/A - This is a test suite.

Returns: N/A - This is a test suite.

Throws: N/A - This is a test suite.

Example: N/A - This is a test suite.

Tests:

  • returns true for supported file extensions: This test checks that isSupported correctly returns true for files with extensions (.js, .jsx, .duck) that are considered supported based on the mocked getLanguages API response.
    • It mocks getLanguages to return a specific set of supported file extensions.
    • It calls helpers.loadConfig() to ensure the configuration is loaded before testing isSupported.
    • It asserts that isSupported returns true for each supported file extension.
  • returns false for unsupported file extensions: This test checks that isSupported correctly returns false for files with extensions (.txt, .doc, .pdf) that are not considered supported based on the mocked getLanguages API response.
    • It asserts that isSupported returns false for each unsupported file extension.

Method: redirect

Description: This test suite checks the functionality of the redirect method from the Helpers class, which handles redirecting the user to a different URL.

Parameters: N/A - This is a test suite.

Returns: N/A - This is a test suite.

Throws: N/A - This is a test suite.

Example: N/A - This is a test suite.

Tests:

  • creates a new tab when asked to: This test verifies that the redirect method opens a new tab in the browser when the newTab argument is set to true.
    • It uses jest.fn() to mock the window.open function.
    • It calls helpers.redirect with the URL / and true for newTab.
    • It asserts that the mocked open function was called, indicating a new tab was opened.
  • opens in the same new tab by default: This test checks that the redirect method opens in the same tab by default when the newTab argument is not provided or is false.
    • It asserts that the mocked open function was not called, indicating that no new tab was opened.

Method: getDuration

Description: This test suite verifies the functionality of the getDuration method from the Helpers class, which calculates the duration between two timestamps.

Parameters: N/A - This is a test suite.

Returns: N/A - This is a test suite.

Throws: N/A - This is a test suite.

Example: N/A - This is a test suite.

Tests:

  • returns the time difference between creation and update in seconds: This test ensures that getDuration accurately calculates the duration in seconds between two timestamps (started_at and finished_at) when the seconds argument is true.
    • It creates a pipe object with simulated timestamps.
    • It calls helpers.getDuration with the pipe object and true for seconds.
    • It asserts that the returned duration is 60 seconds.
  • returns the time difference between creation and update in a human-readable format: This test verifies that getDuration returns a human-readable representation of the duration when the seconds argument is not provided or is false.
    • It creates a pipe object with simulated timestamps.
    • It calls helpers.getDuration with the pipe object.
    • It asserts that the returned duration is "a minute".

Method: formatDateTime

Description: This test suite verifies the functionality of the formatDateTime method from the Helpers class, which formats a date and time into a human-readable string.

Parameters: N/A - This is a test suite.

Returns: N/A - This is a test suite.

Throws: N/A - This is a test suite.

Example: N/A - This is a test suite.

Tests:

  • returns the date time in a human-readable format: This test ensures that formatDateTime correctly formats a date and time.
    • It creates a Date object.
    • It calls helpers.formatTime to format the date and time.
    • It splits the formatted string into words.
    • It asserts that there are two words (date and time), and that they have the expected lengths (10 and 8 characters, respectively).

Method: delay

Description: This test suite checks the functionality of the delay method from the Helpers class, which introduces a delay before resolving a promise.

Parameters: N/A - This is a test suite.

Returns: N/A - This is a test suite.

Throws: N/A - This is a test suite.

Example: N/A - This is a test suite.

Tests:

  • waits for the specified time before resolving: This test ensures that delay correctly waits for the specified time before resolving the promise.
    • It mocks timers using jest.useFakeTimers().
    • It creates a mock callback function.
    • It calls helpers.delay with a delay of 1000 milliseconds.
    • It asserts that the callback function is not called immediately.
    • It advances time using jest.advanceTimersByTime(1000).
    • It waits for the promise to resolve using await Promise.resolve().
    • It asserts that the callback function was called after the delay.

Method: sort

Description: This test suite verifies the functionality of the sort method from the Helpers class, which sorts an array of objects based on a specified key.

Parameters: N/A - This is a test suite.

Returns: N/A - This is a test suite.

Throws: N/A - This is a test suite.

Example: N/A - This is a test suite.

Tests:

  • sorts an array of objects based on the specified key: This test ensures that sort correctly sorts an array of objects in ascending order based on the specified key.
    • It creates an array of objects with name properties.
    • It calls helpers.sort to sort the array based on the name key.
    • It asserts that the sorted array is in the expected order.
  • sorts an array of objects based on the specified key in reverse order: This test checks that sort correctly sorts an array of objects in descending order when the reverse argument is true.
    • It creates an array of objects with name properties.
    • It calls helpers.sort to sort the array based on the name key in reverse order.
    • It asserts that the sorted array is in the expected reverse order.

Method: divide

Description: This test suite verifies the functionality of the divide method from the Helpers class, which divides an array into chunks of a specified size.

Parameters: N/A - This is a test suite.

Returns: N/A - This is a test suite.

Throws: N/A - This is a test suite.

Example: N/A - This is a test suite.

Tests:

  • divides an array into chunks of the specified size: This test ensures that divide correctly divides an array into chunks of the specified size.
    • It creates an array of numbers.
    • It calls helpers.divide to divide the array into chunks of size 3.
    • It asserts that the divided array contains the expected chunks.
  • divides an array of objects into chunks of the specified size based on the specified key: This test checks that divide correctly divides an array of objects into chunks based on the specified key.
    • It creates an array of objects with a num property.
    • It calls helpers.divide to divide the array into chunks of size 3 based on the num key.
    • It asserts that the divided array contains the expected chunks based on the num property values.

==========