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 returnstrue
for files with extensions (.js
,.jsx
,.duck
) that are considered supported based on the mockedgetLanguages
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 testingisSupported
. - It asserts that
isSupported
returnstrue
for each supported file extension.
- It mocks
- returns false for unsupported file extensions: This test checks that
isSupported
correctly returnsfalse
for files with extensions (.txt
,.doc
,.pdf
) that are not considered supported based on the mockedgetLanguages
API response.- It asserts that
isSupported
returnsfalse
for each unsupported file extension.
- It asserts that
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 thenewTab
argument is set totrue
.- It uses
jest.fn()
to mock thewindow.open
function. - It calls
helpers.redirect
with the URL/
andtrue
fornewTab
. - It asserts that the mocked
open
function was called, indicating a new tab was opened.
- It uses
- opens in the same new tab by default: This test checks that the
redirect
method opens in the same tab by default when thenewTab
argument is not provided or isfalse
.- It asserts that the mocked
open
function was not called, indicating that no new tab was opened.
- It asserts that the mocked
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
andfinished_at
) when theseconds
argument istrue
.- It creates a
pipe
object with simulated timestamps. - It calls
helpers.getDuration
with thepipe
object andtrue
forseconds
. - It asserts that the returned duration is 60 seconds.
- It creates a
- 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 theseconds
argument is not provided or isfalse
.- It creates a
pipe
object with simulated timestamps. - It calls
helpers.getDuration
with thepipe
object. - It asserts that the returned duration is "a minute".
- It creates a
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).
- It creates a
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.
- It mocks timers using
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 thename
key. - It asserts that the sorted array is in the expected order.
- It creates an array of objects with
- 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 thereverse
argument istrue
.- It creates an array of objects with
name
properties. - It calls
helpers.sort
to sort the array based on thename
key in reverse order. - It asserts that the sorted array is in the expected reverse order.
- It creates an array of objects with
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 thenum
key. - It asserts that the divided array contains the expected chunks based on the
num
property values.
- It creates an array of objects with a
==========