From f5c8ff31f121e550f81b2472f2e3f0c8d76aab1b Mon Sep 17 00:00:00 2001
From: Erez Rokah <erezrokah@users.noreply.github.com>
Date: Mon, 11 Nov 2019 11:30:34 +0200
Subject: [PATCH] fix(media-libs): accept string or string array for
 insertMedia action (#2857)

---
 .../actions/__tests__/mediaLibrary.spec.js    | 35 ++++++++++++++-----
 .../src/actions/mediaLibrary.js               |  4 ++-
 2 files changed, 30 insertions(+), 9 deletions(-)

diff --git a/packages/netlify-cms-core/src/actions/__tests__/mediaLibrary.spec.js b/packages/netlify-cms-core/src/actions/__tests__/mediaLibrary.spec.js
index a3ce73d8..60dfae12 100644
--- a/packages/netlify-cms-core/src/actions/__tests__/mediaLibrary.spec.js
+++ b/packages/netlify-cms-core/src/actions/__tests__/mediaLibrary.spec.js
@@ -8,7 +8,7 @@ const mockStore = configureMockStore(middlewares);
 
 describe('mediaLibrary', () => {
   describe('insertMedia', () => {
-    it('test public URL is returned directly', () => {
+    it('should return url when input is an object with url property', () => {
       const store = mockStore({});
       store.dispatch(insertMedia({ url: '//localhost/foo.png' }));
       expect(store.getActions()[0]).toEqual({
@@ -17,7 +17,7 @@ describe('mediaLibrary', () => {
       });
     });
 
-    it('Test relative path resolution', () => {
+    it('should resolve to relative path when media_folder_relative is true and object with name property is given', () => {
       const store = mockStore({
         config: fromJS({
           media_folder_relative: true,
@@ -41,8 +41,7 @@ describe('mediaLibrary', () => {
       });
     });
 
-    // media_folder_relative will be used even if public_folder is specified
-    it('Test relative path resolution, with public folder specified', () => {
+    it('should resolve to relative path and ignore public_folder when media_folder_relative is true', () => {
       const store = mockStore({
         config: fromJS({
           media_folder_relative: true,
@@ -67,7 +66,7 @@ describe('mediaLibrary', () => {
       });
     });
 
-    it('Test public_folder resolution', () => {
+    it('should not resolve to relative path when media_folder_relative is not true', () => {
       const store = mockStore({
         config: fromJS({
           public_folder: '/static/assets/media',
@@ -80,14 +79,34 @@ describe('mediaLibrary', () => {
       });
     });
 
-    it('Test incorrect usage', () => {
+    it('should return mediaPath as string when string is given', () => {
+      const store = mockStore({});
+      store.dispatch(insertMedia('foo.png'));
+      expect(store.getActions()[0]).toEqual({
+        type: 'MEDIA_INSERT',
+        payload: { mediaPath: 'foo.png' },
+      });
+    });
+
+    it('should return mediaPath as array of strings when array of strings is given', () => {
+      const store = mockStore({});
+      store.dispatch(insertMedia(['foo.png']));
+      expect(store.getActions()[0]).toEqual({
+        type: 'MEDIA_INSERT',
+        payload: { mediaPath: ['foo.png'] },
+      });
+    });
+
+    it('should throw an error when not a object with url or name property, a string or a string array', () => {
       const store = mockStore();
 
+      expect.assertions(1);
       try {
         store.dispatch(insertMedia({ foo: 'foo.png' }));
-        throw new Error('Expected Exception');
       } catch (e) {
-        expect(e.message).toEqual('Incorrect usage, expected {url} or {file}');
+        expect(e.message).toEqual(
+          'Incorrect usage, expected {url}, {file}, string or string array',
+        );
       }
     });
   });
diff --git a/packages/netlify-cms-core/src/actions/mediaLibrary.js b/packages/netlify-cms-core/src/actions/mediaLibrary.js
index f1671842..c3e1f6a8 100644
--- a/packages/netlify-cms-core/src/actions/mediaLibrary.js
+++ b/packages/netlify-cms-core/src/actions/mediaLibrary.js
@@ -104,8 +104,10 @@ export function insertMedia(media) {
         const publicFolder = config.get('public_folder');
         mediaPath = resolveMediaFilename(media.name, { publicFolder });
       }
+    } else if (Array.isArray(media) || typeof media === 'string') {
+      mediaPath = media;
     } else {
-      throw new Error('Incorrect usage, expected {url} or {file}');
+      throw new Error('Incorrect usage, expected {url}, {file}, string or string array');
     }
     dispatch({ type: MEDIA_INSERT, payload: { mediaPath } });
   };