Skip to content

Commit

Permalink
State: Handle fetched settings and missing site in getSiteDefaultPost…
Browse files Browse the repository at this point in the history
…Format
  • Loading branch information
tyxla committed Apr 28, 2017
1 parent 258ee6e commit 6a14585
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
6 changes: 3 additions & 3 deletions client/state/selectors/get-site-default-post-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import { getSiteSettings } from 'state/site-settings/selectors';
* @return {?String} The default post format of that site
*/
export default function getSiteDefaultPostFormat( state, siteId ) {
const site = getRawSite( state, siteId );
if ( ! site ) {
const siteSettings = getSiteSettings( state, siteId );
if ( ! siteSettings && ! getRawSite( state, siteId ) ) {
return null;
}

let defaultPostFormat = get( getSiteSettings( state, siteId ), 'default_post_format' );
let defaultPostFormat = get( siteSettings, 'default_post_format' );
if ( ! defaultPostFormat ) {
defaultPostFormat = getSiteOption( state, siteId, 'default_post_format' );
}
Expand Down
58 changes: 55 additions & 3 deletions client/state/selectors/test/get-site-default-post-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { getSiteDefaultPostFormat } from '../';
describe( 'getSiteDefaultPostFormat()', () => {
const siteId = 2916284;

it( 'should return default post format for a known site', () => {
it( 'should return default post format for a known site when settings have not been fetched', () => {
const state = {
sites: {
items: {
Expand All @@ -27,7 +27,24 @@ describe( 'getSiteDefaultPostFormat()', () => {
expect( output ).to.eql( 'image' );
} );

it( 'should prioritize default post format from settings', () => {
it( 'should return default post format when settings have been fetched and site is unknown', () => {
const state = {
siteSettings: {
items: {
[ siteId ]: {
default_post_format: 'aside',
},
},
},
sites: {
items: {}
}
};
const output = getSiteDefaultPostFormat( state, siteId );
expect( output ).to.eql( 'aside' );
} );

it( 'should prioritize default post format from settings over post format from sites', () => {
const state = {
siteSettings: {
items: {
Expand Down Expand Up @@ -66,6 +83,22 @@ describe( 'getSiteDefaultPostFormat()', () => {
expect( output ).to.eql( 'standard' );
} );

it( 'should return standard if post format is set to an empty string', () => {
const state = {
sites: {
items: {
[ siteId ]: {
options: {
default_post_format: '',
}
},
}
}
};
const output = getSiteDefaultPostFormat( state, siteId );
expect( output ).to.eql( 'standard' );
} );

it( 'should return standard if post format is missing for a known site', () => {
const state = {
sites: {
Expand All @@ -82,7 +115,26 @@ describe( 'getSiteDefaultPostFormat()', () => {
expect( output ).to.eql( 'standard' );
} );

it( 'should return null for an unknown site', () => {
it( 'should return standard if settings are fetched, but post format option is missing', () => {
const state = {
siteSettings: {
items: {
[ siteId ]: {
options: {
some_option: 'example',
}
},
},
},
sites: {
items: {}
}
};
const output = getSiteDefaultPostFormat( state, siteId );
expect( output ).to.eql( 'standard' );
} );

it( 'should return null for an unknown site when settings have not been fetched', () => {
const state = {
sites: {
items: {
Expand Down

0 comments on commit 6a14585

Please sign in to comment.