Skip to content

Commit

Permalink
Merge pull request #11131 from Automattic/update/timezones-state-sele…
Browse files Browse the repository at this point in the history
…ctors

Add timezones selectors and tests
  • Loading branch information
retrofox authored Feb 8, 2017
2 parents 0620965 + b691847 commit 340b844
Show file tree
Hide file tree
Showing 14 changed files with 497 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ import { get } from 'lodash';
* @param {Object} state - Global state tree
* @return {?Array} An array of manual offset timezones
*/
export default function getManualUtcOffsets( state ) {
return get( state, 'timezones.items.manual_utc_offsets', null );
export default function getRawOffsets( state ) {
return get( state, 'timezones.rawOffsets', null );
}
10 changes: 6 additions & 4 deletions client/state/selectors/get-timezones-by-continent.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import { get } from 'lodash';
* Return the timezones by continent data
* gotten from state.timezones subtree.
*
* @param {Object} state - Global state tree
* @return {Array} An object with the timezones grouped by continents
* @param {Object} state - Global state tree
* @param {String} continent - continent value
* @return {Array} Continent timezones array
*/
export default function getTimezonesByContinent( state ) {
return get( state, 'timezones.items.timezones_by_continent', null );
export default function getTimezonesByContinent( state, continent ) {
const byContinents = get( state, 'timezones.byContinents', {} );
return byContinents[ continent ] ? byContinents[ continent ] : null;
}
16 changes: 16 additions & 0 deletions client/state/selectors/get-timezones-label.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Internal dependencies
*/
import { getTimezonesLabels } from 'state/selectors';

/**
* Return timezone `label` according to the given timezone key (value)
*
* @param {Object} state - Global state tree
* @param {String} key - timezone value
* @return {String} the timezone label
*/
export default function getTimezonesLabel( state, key ) {
const labels = getTimezonesLabels( state );
return labels[ key ] ? labels[ key ] : null;
}
33 changes: 33 additions & 0 deletions client/state/selectors/get-timezones-labels-by-continent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* External dependencies
*/
import {
fromPairs,
map,
} from 'lodash';

/**
* Internal dependencies
*/
import {
getTimezonesByContinent,
getTimezonesLabel
} from 'state/selectors/';

/**
* Return the timezones by continent data
* gotten from state.timezones subtree.
*
* @param {Object} state - Global state tree
* @param {String} continent - continent value
* @return {Array} Continent timezones array
*/
export default function getTimezonesLabelsByContinent( state, continent ) {
const timezones = getTimezonesByContinent( state, continent );

if ( ! timezones ) {
return null;
}

return fromPairs( map( timezones, value => ( [ value, getTimezonesLabel( state, value ) ] ) ) );
}
17 changes: 17 additions & 0 deletions client/state/selectors/get-timezones-labels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* External dependencies
*/
import get from 'lodash/get';

/**
* Return an object of timezones.
* Each element is has the shape `[ value ]: label`.
* The `value` is the timezone-value used to data processing,
* and the `label` is the value used for the UI.
*
* @param {Object} state - Global state tree
* @return {Object} An object of timezones labels
*/
export default function getTimezonesLabels( state ) {
return get( state, 'timezones.labels', {} );
}
38 changes: 38 additions & 0 deletions client/state/selectors/get-timezones.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* External dependencies
*/
import {
get,
map,
toPairs,
} from 'lodash';

/**
* Internal dependencies
*/
import { getTimezonesLabel } from 'state/selectors/';

/**
* Return all timezones ordered by arrays with
* the following shape:
* [
* [ <continent>, [
* [ <timezone-value>, <timezone-label> ],
* ] ]
* ...
* ]
*
* This structure facilitates the creation of a select element.
*
* @param {Object} state - Global state tree
* @return {Array} Timezones arrays
*/
export default function getTimezones( state ) {
const continents = toPairs( get( state, 'timezones.byContinents', null ) );

if ( ! continents ) {
return null;
}

return map( continents, zones => [ zones[ 0 ], map( zones[ 1 ], value => [ value, getTimezonesLabel( state, value ) ] ) ] );
}
7 changes: 5 additions & 2 deletions client/state/selectors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,23 @@ export getJetpackSetting from './get-jetpack-setting';
export getJetpackSettings from './get-jetpack-settings';
export getJetpackSettingsSaveError from './get-jetpack-settings-save-error';
export getJetpackSettingsSaveRequestStatus from './get-jetpack-settings-save-request-status';
export getManualUtcOffsets from './get-manual-utc-offsets';
export getMedia from './get-media';
export getMediaItem from './get-media-item';
export getMediaUrl from './get-media-url';
export getMenuItemTypes from './get-menu-item-types';
export getPastBillingTransaction from './get-past-billing-transaction';
export getPastBillingTransactions from './get-past-billing-transactions';
export getPostLikes from './get-post-likes';
export getRawOffsets from './get-raw-offsets';
export getReaderTeams from './get-reader-teams';
export getSharingButtons from './get-sharing-buttons';
export getSiteIconId from './get-site-icon-id';
export getSiteIconUrl from './get-site-icon-url';
export getTimezones from './get-timezones';
export getTimezonesByContinent from './get-timezones-by-continent';
export getTimezonesLabel from './get-timezones-label';
export getTimezonesLabels from './get-timezones-labels';
export getTimezonesLabelsByContinent from './get-timezones-labels-by-continent';
export getUpcomingBillingTransactions from './get-upcoming-billing-transactions';
export hasBrokenSiteUserConnection from './has-broken-site-user-connection';
export isActivatingJetpackJumpstart from './is-activating-jetpack-jumpstart';
Expand All @@ -67,7 +71,6 @@ export isRequestingMediaItem from './is-requesting-media-item';
export isRequestingPostLikes from './is-requesting-post-likes';
export isRequestingReaderTeams from './is-requesting-reader-teams';
export isRequestingSharingButtons from './is-requesting-sharing-buttons';
export isRequestingTimezones from './is-requesting-timezones';
export isSavingSharingButtons from './is-saving-sharing-buttons';
export isSendingBillingReceiptEmail from './is-sending-billing-receipt-email';
export isSharingButtonsSaveSuccessful from './is-sharing-buttons-save-successful';
Expand Down
14 changes: 0 additions & 14 deletions client/state/selectors/is-requesting-timezones.js

This file was deleted.

47 changes: 47 additions & 0 deletions client/state/selectors/test/get-raw-offsets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* External dependencies
*/
import { expect } from 'chai';

/**
* Internal dependencies
*/
import { getRawOffsets } from '../';

describe( 'getRawOffsets()', () => {
it( 'should return null if `timezones` aren\'t synced', () => {
const state = {
timezones: {
byContinents: {},
labels: {},
rawOffsets: {},
}
};

const manualUTCOffsets = getRawOffsets( state );

expect( manualUTCOffsets ).to.eql( {} );
} );

it( 'should return raw offsets data', () => {
const state = {
timezones: {
rawOffsets: {
'UTC+0': 'UTC',
'UTC-12': 'UTC-12',
'UTC-11.5': 'UTC-11:30',
},
labels: {},
byContinent: {},
}
};

const offsets = getRawOffsets( state );

expect( offsets ).to.eql( {
'UTC+0': 'UTC',
'UTC-12': 'UTC-12',
'UTC-11.5': 'UTC-11:30',
} );
} );
} );
76 changes: 76 additions & 0 deletions client/state/selectors/test/get-timezones-by-continent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* External dependencies
*/
import { expect } from 'chai';

/**
* Internal dependencies
*/
import { getTimezonesByContinent } from '../';

describe( 'getTimezonesByContinent()', () => {
it( 'should return null if `timezones` aren\'t synced', () => {
const state = {
timezones: {
byContinents: {},
labels: {},
rawOffsets: {},
}
};

const byContinent = getTimezonesByContinent( state, 'Atlantic' );
expect( byContinent ).to.eql( null );
} );

it( 'should return null if `continent` isn\'t defined', () => {
const state = {
timezones: {
byContinents: {
Asia: [
'Asia/Aqtobe',
],
America: [
'America/Blanc-Sablon',
'America/Boa_Vista',
],
Indian: [
'Indian/Comoro',
],
},
labels: {},
rawOffsets: {},
}
};

const byContinent = getTimezonesByContinent( state );
expect( byContinent ).to.eql( null );
} );

it( 'should return timezones by contienent object data', () => {
const state = {
timezones: {
byContinents: {
Asia: [
'Asia/Aqtobe',
],
America: [
'America/Blanc-Sablon',
'America/Boa_Vista',
],
Indian: [
'Indian/Comoro',
],
},
labels: {},
rawOffsets: {},
}
};

const byContinent = getTimezonesByContinent( state, 'America' );

expect( byContinent ).to.eql( [
'America/Blanc-Sablon',
'America/Boa_Vista',
] );
} );
} );
59 changes: 59 additions & 0 deletions client/state/selectors/test/get-timezones-label.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* External dependencies
*/
import { expect } from 'chai';

/**
* Internal dependencies
*/
import { getTimezonesLabel } from '../';

describe( 'getTimezonesLabel()', () => {
it( 'should return null if `timezones` aren\'t synced', () => {
const state = {
timezones: {
byContinents: {},
labels: {},
rawOffsets: {},
}
};

const label = getTimezonesLabel( state );

expect( label ).to.eql( null );
} );

it( 'should return null if `key` isn\'t defined', () => {
const state = {
timezones: {
byContinents: {},
labels: {
'Asia/Aqtobe': 'Aqtobe',
'America/Boa_Vista': 'Boa Vista',
'Indian/Comoro': 'Comoro',
},
rawOffsets: {},
}
};

const label = getTimezonesLabel( state );
expect( label ).to.eql( null );
} );

it( 'should return the label of the given key', () => {
const state = {
timezones: {
byContinents: {},
labels: {
'Asia/Aqtobe': 'Aqtobe',
'America/Boa_Vista': 'Boa Vista',
'Indian/Comoro': 'Comoro',
},
rawOffsets: {},
}
};

const label = getTimezonesLabel( state, 'America/Boa_Vista' );
expect( label ).to.eql( 'Boa Vista' );
} );
} );
Loading

0 comments on commit 340b844

Please sign in to comment.