-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11131 from Automattic/update/timezones-state-sele…
…ctors Add timezones selectors and tests
- Loading branch information
Showing
14 changed files
with
497 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
client/state/selectors/get-timezones-labels-by-continent.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) ] ) ) ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', {} ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) ] ) ] ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
} ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
] ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ); | ||
} ); | ||
} ); |
Oops, something went wrong.