From 6a1458528b2cfa34645b4295afa0c41e3d756644 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Fri, 28 Apr 2017 13:48:23 +0300 Subject: [PATCH] State: Handle fetched settings and missing site in getSiteDefaultPostFormat --- .../selectors/get-site-default-post-format.js | 6 +- .../test/get-site-default-post-format.js | 58 ++++++++++++++++++- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/client/state/selectors/get-site-default-post-format.js b/client/state/selectors/get-site-default-post-format.js index 5b3de62b13ace..4fbbb1903bd1a 100644 --- a/client/state/selectors/get-site-default-post-format.js +++ b/client/state/selectors/get-site-default-post-format.js @@ -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' ); } diff --git a/client/state/selectors/test/get-site-default-post-format.js b/client/state/selectors/test/get-site-default-post-format.js index 57106e5452126..79f5271090dfa 100644 --- a/client/state/selectors/test/get-site-default-post-format.js +++ b/client/state/selectors/test/get-site-default-post-format.js @@ -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: { @@ -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: { @@ -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: { @@ -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: {