Skip to content

Commit

Permalink
fix: missing jsdocs + update tests query selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
sjdonado committed Jul 21, 2024
1 parent a2e25e0 commit fb82513
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
14 changes: 13 additions & 1 deletion src/views/controllers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ import { Notyf } from 'notyf';

let _toast;

/**
* Initializes and returns a Notyf instance for displaying toast notifications.
*
* @returns {Notyf} The Notyf instance for toast notifications.
*/
export const toast = () => {
if (_toast) return _toast;

_toast = new Notyf({
riple: false,
ripple: false,
dismissible: true,
duration: 2000,
types: [
Expand All @@ -20,6 +25,13 @@ export const toast = () => {
return _toast;
};

/**
* Copies the provided link to the clipboard and shows a success toast notification.
* If the clipboard API is not available, it falls back to using a temporary textarea.
*
* @param {string} link - The link to copy to the clipboard.
* @returns {Promise<void>}
*/
export const copyToClipboard = async link => {
try {
if (navigator.clipboard) {
Expand Down
20 changes: 19 additions & 1 deletion src/views/controllers/search_card_controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Controller } from '@hotwired/stimulus';
import { Howl } from 'howler';

import { copyToClipboard } from './helpers';

export default class extends Controller {
Expand Down Expand Up @@ -28,6 +27,9 @@ export default class extends Controller {
});
}

/**
* Shares the universal link using the Web Share API or copies it to the clipboard.
*/
async share() {
const universalLink = this.universalLinkValue;

Expand All @@ -51,6 +53,9 @@ export default class extends Controller {
}
}

/**
* Toggles the audio playback state and updates the play/pause icon.
*/
toggleAudio() {
const isPlaying = this.soundPlayer.playing();
if (isPlaying) {
Expand All @@ -61,6 +66,9 @@ export default class extends Controller {
this.updateAudioPreviewIcon(isPlaying);
}

/**
* Starts updating the audio progress bar.
*/
startAudioProgress() {
this.audioProgressInterval = setInterval(() => {
const duration = this.soundPlayer.duration();
Expand All @@ -70,15 +78,25 @@ export default class extends Controller {
}, 10);
}

/**
* Stops updating the audio progress bar.
*/
stopProgressUpdate() {
clearInterval(this.audioProgressInterval);
}

/**
* Resets the audio progress bar.
*/
resetProgressBar() {
clearInterval(this.audioProgressInterval);
this.audioProgressTarget.style.width = '0%';
}

/**
* Updates the audio preview icon based on the playback state.
* @param {boolean} playing - Indicates if the audio is currently playing.
*/
updateAudioPreviewIcon(playing) {
const iconElement = this.iconTarget;
if (playing) {
Expand Down
6 changes: 6 additions & 0 deletions src/views/controllers/search_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ export default class extends Controller {
});
}

/**
* Submits the form using a link obtained from the clipboard if it matches
* specific patterns and no search ID is present in the URL.
*
* @returns {Promise<void>}
*/
async submitFromClipboard() {
if ('clipboard' in navigator) {
const searchParams = new URLSearchParams(window.location.search);
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/page.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ describe('Page router', () => {

const doc = getCheerioDoc(response);

const searchCardText = doc('#search-card').text();
const searchCardText = doc('[data-controller="search-card"]').text();

expect(searchCardText).toContain('Do Not Disturb');
expect(searchCardText).toContain('Drake · Song · 2017');

const searchLinks = doc('#search-card > div.flex-1 > ul > a').toArray();
const searchLinks = doc('[data-controller="search-link"] > a').toArray();

expect(searchLinks).toHaveLength(5);
expect(searchLinks[0].attribs['aria-label']).toContain('Listen on YouTube');
Expand Down Expand Up @@ -141,7 +141,7 @@ describe('Page router', () => {

const doc = getCheerioDoc(response);

const searchCardText = doc('#search-card').text();
const searchCardText = doc('[data-controller="search-card"]').text();

expect(searchCardText).toContain('Do Not Disturb');
expect(searchCardText).toContain('Drake · Song · 2017');
Expand Down Expand Up @@ -178,7 +178,7 @@ describe('Page router', () => {
const doc = getCheerioDoc(response);

const errorMessage = doc('p').text();
expect(errorMessage).toContain('Something went wrong, try again later.');
expect(errorMessage).toContain('Something went wrong, please try again later.');

expect(getSearchParserMock).toHaveBeenCalledTimes(1);
});
Expand Down

0 comments on commit fb82513

Please sign in to comment.