refactor Settings

This commit is contained in:
Jeremie Pardou-Piquemal 2022-12-15 22:52:54 +01:00
parent c9fa5599b4
commit 7eb31f13e3
3 changed files with 192 additions and 171 deletions

View File

@ -1,4 +1,3 @@
import React from 'react';
import { render, fireEvent, waitFor } from '@testing-library/react'; import { render, fireEvent, waitFor } from '@testing-library/react';
import { Provider } from 'react-redux'; import { Provider } from 'react-redux';
import { describe, it, expect, vi } from 'vitest'; import { describe, it, expect, vi } from 'vitest';
@ -9,10 +8,6 @@ import Settings from '.';
const store = configureStore(); const store = configureStore();
const mockTranslations = {
sound: 'soundCheck',
};
vi.useFakeTimers(); vi.useFakeTimers();
vi.mock('@/components/RoomLink'); vi.mock('@/components/RoomLink');
@ -39,11 +34,14 @@ describe('Settings component', () => {
<Settings <Settings
soundIsEnabled={true} soundIsEnabled={true}
toggleSoundEnabled={() => {}} toggleSoundEnabled={() => {}}
persistenceIsEnabled={true}
togglePersistenceEnabled={() => {}}
notificationIsEnabled={true} notificationIsEnabled={true}
toggleNotificationEnabled={() => {}} toggleNotificationEnabled={() => {}}
toggleNotificationAllowed={vi.fn()} toggleNotificationAllowed={vi.fn()}
roomId="roomId" roomId="roomId"
setLanguage={() => {}} setLanguage={() => {}}
language="en"
translations={{}} translations={{}}
/> />
</Provider>, </Provider>,
@ -56,12 +54,15 @@ describe('Settings component', () => {
<Settings <Settings
soundIsEnabled={true} soundIsEnabled={true}
toggleSoundEnabled={() => {}} toggleSoundEnabled={() => {}}
persistenceIsEnabled={true}
togglePersistenceEnabled={() => {}}
notificationIsEnabled={true} notificationIsEnabled={true}
notificationIsAllowed={false} notificationIsAllowed={false}
toggleNotificationEnabled={() => {}} toggleNotificationEnabled={() => {}}
toggleNotificationAllowed={vi.fn()} toggleNotificationAllowed={vi.fn()}
roomId="roomId" roomId="roomId"
setLanguage={() => {}} setLanguage={() => {}}
language="en"
translations={{}} translations={{}}
/> />
</Provider>, </Provider>,
@ -77,18 +78,20 @@ describe('Settings component', () => {
<Settings <Settings
soundIsEnabled={true} soundIsEnabled={true}
toggleSoundEnabled={toggleSound} toggleSoundEnabled={toggleSound}
persistenceIsEnabled={true}
togglePersistenceEnabled={() => {}}
notificationIsEnabled={true} notificationIsEnabled={true}
notificationIsAllowed={true} notificationIsAllowed={true}
toggleNotificationEnabled={() => {}} toggleNotificationEnabled={() => {}}
toggleNotificationAllowed={vi.fn()} toggleNotificationAllowed={vi.fn()}
roomId="roomId" roomId="roomId"
setLanguage={() => {}} setLanguage={() => {}}
language="en"
translations={{}} translations={{}}
/> />
</Provider>, </Provider>,
); );
//console.log(getAllByText(mockTranslations.sound)[1]);
fireEvent.click(getByText('Sound')); fireEvent.click(getByText('Sound'));
expect(toggleSound).toHaveBeenCalledWith(false); expect(toggleSound).toHaveBeenCalledWith(false);
@ -105,12 +108,15 @@ describe('Settings component', () => {
<Settings <Settings
soundIsEnabled={true} soundIsEnabled={true}
toggleSoundEnabled={() => {}} toggleSoundEnabled={() => {}}
persistenceIsEnabled={true}
togglePersistenceEnabled={() => {}}
notificationIsEnabled={true} notificationIsEnabled={true}
notificationIsAllowed={true} notificationIsAllowed={true}
toggleNotificationEnabled={toggleNotifications} toggleNotificationEnabled={toggleNotifications}
toggleNotificationAllowed={vi.fn()} toggleNotificationAllowed={vi.fn()}
roomId="roomId" roomId="roomId"
setLanguage={() => {}} setLanguage={() => {}}
language="en"
translations={{}} translations={{}}
/> />
</Provider>, </Provider>,
@ -137,12 +143,15 @@ describe('Settings component', () => {
<Settings <Settings
soundIsEnabled={true} soundIsEnabled={true}
toggleSoundEnabled={() => {}} toggleSoundEnabled={() => {}}
persistenceIsEnabled={true}
togglePersistenceEnabled={() => {}}
notificationIsEnabled={true} notificationIsEnabled={true}
notificationIsAllowed={true} notificationIsAllowed={true}
toggleNotificationEnabled={toggleNotifications} toggleNotificationEnabled={toggleNotifications}
toggleNotificationAllowed={toggleAllowed} toggleNotificationAllowed={toggleAllowed}
roomId="roomId" roomId="roomId"
setLanguage={() => {}} setLanguage={() => {}}
language="en"
translations={{}} translations={{}}
/> />
</Provider>, </Provider>,
@ -166,11 +175,14 @@ describe('Settings component', () => {
<Settings <Settings
soundIsEnabled={true} soundIsEnabled={true}
toggleSoundEnabled={() => {}} toggleSoundEnabled={() => {}}
persistenceIsEnabled={true}
togglePersistenceEnabled={() => {}}
notificationIsEnabled={true} notificationIsEnabled={true}
toggleNotificationEnabled={() => {}} toggleNotificationEnabled={() => {}}
toggleNotificationAllowed={vi.fn()} toggleNotificationAllowed={vi.fn()}
roomId="roomId" roomId="roomId"
setLanguage={changeLang} setLanguage={changeLang}
language="en"
translations={{}} translations={{}}
/> />
</Provider>, </Provider>,

View File

@ -50,6 +50,7 @@ exports[`Settings component > should display 1`] = `
for="persistence-control" for="persistence-control"
> >
<input <input
checked=""
class="form-check-input" class="form-check-input"
id="persistence-control" id="persistence-control"
type="checkbox" type="checkbox"
@ -261,6 +262,7 @@ exports[`Settings component > should display 2`] = `
for="persistence-control" for="persistence-control"
> >
<input <input
checked=""
class="form-check-input" class="form-check-input"
id="persistence-control" id="persistence-control"
type="checkbox" type="checkbox"

View File

@ -1,4 +1,3 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import RoomLink from '@/components/RoomLink'; import RoomLink from '@/components/RoomLink';
@ -6,33 +5,44 @@ import T from '@/components/T';
import classes from './styles.module.scss'; import classes from './styles.module.scss';
class Settings extends Component { const Settings = ({
handleSoundToggle() { soundIsEnabled,
this.props.toggleSoundEnabled(!this.props.soundIsEnabled); persistenceIsEnabled,
} toggleSoundEnabled,
notificationIsEnabled,
notificationIsAllowed,
toggleNotificationEnabled,
toggleNotificationAllowed,
togglePersistenceEnabled,
roomId,
setLanguage,
language,
translations,
}) => {
const handleSoundToggle = () => {
toggleSoundEnabled(!soundIsEnabled);
};
handlePersistenceToggle() { const handlePersistenceToggle = () => {
this.props.togglePersistenceEnabled(!this.props.persistenceIsEnabled); togglePersistenceEnabled(!persistenceIsEnabled);
} };
handleNotificationToggle() { const handleNotificationToggle = () => {
Notification.requestPermission().then(permission => { Notification.requestPermission().then(permission => {
if (permission === 'granted') { if (permission === 'granted') {
this.props.toggleNotificationEnabled(!this.props.notificationIsEnabled); toggleNotificationEnabled(!notificationIsEnabled);
this.props.toggleNotificationAllowed(true); toggleNotificationAllowed(true);
} }
if (permission === 'denied') { if (permission === 'denied') {
this.props.toggleNotificationAllowed(false); toggleNotificationAllowed(false);
} }
}); });
} };
handleLanguageChange(evt) { const handleLanguageChange = evt => {
const language = evt.target.value; setLanguage(evt.target.value);
this.props.setLanguage(language); };
}
render() {
return ( return (
<div className={classes.styles}> <div className={classes.styles}>
<section> <section>
@ -44,40 +54,40 @@ class Settings extends Component {
<label className="form-check-label" htmlFor="sound-control"> <label className="form-check-label" htmlFor="sound-control">
<input <input
id="sound-control" id="sound-control"
onChange={this.handleSoundToggle.bind(this)} onChange={handleSoundToggle}
className="form-check-input" className="form-check-input"
type="checkbox" type="checkbox"
checked={this.props.soundIsEnabled} checked={soundIsEnabled}
/> />
<T path="sound" /> <T path="sound" />
</label> </label>
</div> </div>
<div className="form-check"> <div className="form-check">
<label className="form-check-label" htmlFor="notif-control"> <label className="form-check-label" htmlFor="notif-control">
{this.props.notificationIsAllowed !== false && ( {notificationIsAllowed !== false && (
<> <>
<input <input
id="notif-control" id="notif-control"
onChange={this.handleNotificationToggle.bind(this)} onChange={handleNotificationToggle}
className="form-check-input" className="form-check-input"
type="checkbox" type="checkbox"
checked={this.props.notificationIsEnabled} checked={notificationIsEnabled}
disabled={this.props.notificationIsAllowed === false} // Important to keep '=== false' here disabled={notificationIsAllowed === false} // Important to keep '=== false' here
/> />
<T path="desktopNotification" /> <T path="desktopNotification" />
</> </>
)} )}
{this.props.notificationIsAllowed === false && <T path="desktopNotificationBlocked" />} {notificationIsAllowed === false && <T path="desktopNotificationBlocked" />}
</label> </label>
</div> </div>
<div className="form-check"> <div className="form-check">
<label className="form-check-label" htmlFor="persistence-control"> <label className="form-check-label" htmlFor="persistence-control">
<input <input
id="persistence-control" id="persistence-control"
onChange={this.handlePersistenceToggle.bind(this)} onChange={handlePersistenceToggle}
className="form-check-input" className="form-check-input"
type="checkbox" type="checkbox"
checked={this.props.persistenceIsEnabled} checked={persistenceIsEnabled}
/> />
<T path="persistence" /> <T path="persistence" />
</label> </label>
@ -89,7 +99,7 @@ class Settings extends Component {
<h4 className="mb-3"> <h4 className="mb-3">
<T path="copyRoomHeader" /> <T path="copyRoomHeader" />
</h4> </h4>
<RoomLink roomId={this.props.roomId} translations={this.props.translations} /> <RoomLink roomId={roomId} translations={translations} />
</section> </section>
<section> <section>
@ -106,11 +116,7 @@ class Settings extends Component {
</a> </a>
</p> </p>
<div className="form-group"> <div className="form-group">
<select <select value={language} className="form-control" onChange={handleLanguageChange}>
value={this.props.language}
className="form-control"
onChange={this.handleLanguageChange.bind(this)}
>
<option value="en">English</option> <option value="en">English</option>
<option value="fr">Français</option> <option value="fr">Français</option>
<option value="oc">Occitan</option> <option value="oc">Occitan</option>
@ -180,19 +186,20 @@ class Settings extends Component {
</section> </section>
</div> </div>
); );
} };
}
Settings.propTypes = { Settings.propTypes = {
soundIsEnabled: PropTypes.bool.isRequired, soundIsEnabled: PropTypes.bool.isRequired,
persistenceIsEnabled: PropTypes.bool.isRequired, persistenceIsEnabled: PropTypes.bool.isRequired,
toggleSoundEnabled: PropTypes.func.isRequired, toggleSoundEnabled: PropTypes.func.isRequired,
togglePersistenceEnabled: PropTypes.func.isRequired,
notificationIsEnabled: PropTypes.bool.isRequired, notificationIsEnabled: PropTypes.bool.isRequired,
notificationIsAllowed: PropTypes.bool, notificationIsAllowed: PropTypes.bool,
toggleNotificationEnabled: PropTypes.func.isRequired, toggleNotificationEnabled: PropTypes.func.isRequired,
toggleNotificationAllowed: PropTypes.func.isRequired, toggleNotificationAllowed: PropTypes.func.isRequired,
roomId: PropTypes.string.isRequired, roomId: PropTypes.string.isRequired,
setLanguage: PropTypes.func.isRequired, setLanguage: PropTypes.func.isRequired,
language: PropTypes.string.isRequired,
translations: PropTypes.object.isRequired, translations: PropTypes.object.isRequired,
}; };