From 4979c6431f27b82beb4371fcf4f86f5dbe0ce2f6 Mon Sep 17 00:00:00 2001 From: Brian Macdonald Date: Mon, 5 Mar 2018 10:36:04 -0500 Subject: [PATCH 01/17] fix: allows for valid config types expansion --- src/actions/__tests__/config.spec.js | 43 +++++++++++++++++++++++++++- src/actions/config.js | 5 ++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/actions/__tests__/config.spec.js b/src/actions/__tests__/config.spec.js index f32f2d441eb3..165ae17616b8 100644 --- a/src/actions/__tests__/config.spec.js +++ b/src/actions/__tests__/config.spec.js @@ -118,4 +118,45 @@ describe('config', () => { }).toThrowError('Error in configuration file: Your `collections` must be an array with at least one element. Check your config.yml file.'); }); }); -}); \ No newline at end of file + + describe('getConfig', () => { + const validTypes = ['text/yaml', 'application/x-yaml']; + const isValidType = link => link && validTypes.includes(link.type); + beforeEach(() => { + const testChild = document.getElementById('test'); + if (testChild) document.head.removeChild(testChild); + const testLink = document.createElement('link'); + testLink.setAttribute('id', 'test'); + testLink.setAttribute('href', 'the/test/works'); + testLink.setAttribute('rel', 'cms-config-url'); + testLink.setAttribute('type', 'text/yaml'); + document.head.appendChild(testLink); + }); + it('should should return a default url if there is no in .', () => { + const testChild = document.getElementById('test'); + document.head.removeChild(testChild); + const configLink = document.querySelector('link[rel="cms-config-url"]'); + const configUrl = isValidType(configLink) ? get(configLink, 'href') : 'config.yml'; + expect(configUrl).toEqual('config.yml'); + }); + it('should return the href if one is provided.', () => { + const configLink = document.querySelector('link[rel="cms-config-url"]'); + const configUrl = isValidType(configLink) ? get(configLink, 'href') : 'config.yml'; + expect(configUrl).toEqual('the/test/works'); + }); + it('should return the href if it is provided with an alternate type.', () => { + const testChild = document.getElementById('test'); + testChild.setAttribute('type', 'application/x-yaml'); + const configLink = document.querySelector('link[rel="cms-config-url"]'); + const configUrl = isValidType(configLink) ? get(configLink, 'href') : 'config.yml'; + expect(configUrl).toEqual('the/test/works'); + }); + it('should return default if the has an unsupported type.', () => { + const testChild = document.getElementById('test'); + testChild.setAttribute('type', 'wrong/type'); + const configLink = document.querySelector('link[rel="cms-config-url"]'); + const configUrl = isValidType(configLink) ? get(configLink, 'href') : 'config.yml'; + expect(configUrl).toEqual('config.yml'); + }); + }); +}); diff --git a/src/actions/config.js b/src/actions/config.js index 6f272bbab216..731319b4f434 100644 --- a/src/actions/config.js +++ b/src/actions/config.js @@ -9,6 +9,11 @@ export const CONFIG_SUCCESS = "CONFIG_SUCCESS"; export const CONFIG_FAILURE = "CONFIG_FAILURE"; export const CONFIG_MERGE = "CONFIG_MERGE"; +const validTypes = [ "text/yaml", "application/x-yaml" ]; +const configLink = document.querySelector('link[rel="cms-config-url"]'); +const isValidType = link => link && validTypes.includes(link.type); +const configUrl = isValidType(configLink) ? get(configLink, 'href') : 'config.yml'; + const defaults = { publish_mode: publishModes.SIMPLE, }; From 05348f6625aaf30193c7e1fcaee58964b50b8392 Mon Sep 17 00:00:00 2001 From: Brian Macdonald Date: Wed, 28 Feb 2018 11:09:53 -0500 Subject: [PATCH 02/17] feat: config url can now come through link tag --- example/index.html | 3 ++ src/actions/__tests__/config.spec.js | 41 ++++++++++------------------ src/actions/config.js | 24 ++++++++++++++-- 3 files changed, 39 insertions(+), 29 deletions(-) diff --git a/example/index.html b/example/index.html index 1be36224a0ad..98c046951341 100644 --- a/example/index.html +++ b/example/index.html @@ -7,6 +7,9 @@ + + +