fix(media-libs): accept string or string array for insertMedia action (#2857)
This commit is contained in:
parent
c94a2dd3ea
commit
f5c8ff31f1
@ -8,7 +8,7 @@ const mockStore = configureMockStore(middlewares);
|
|||||||
|
|
||||||
describe('mediaLibrary', () => {
|
describe('mediaLibrary', () => {
|
||||||
describe('insertMedia', () => {
|
describe('insertMedia', () => {
|
||||||
it('test public URL is returned directly', () => {
|
it('should return url when input is an object with url property', () => {
|
||||||
const store = mockStore({});
|
const store = mockStore({});
|
||||||
store.dispatch(insertMedia({ url: '//localhost/foo.png' }));
|
store.dispatch(insertMedia({ url: '//localhost/foo.png' }));
|
||||||
expect(store.getActions()[0]).toEqual({
|
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({
|
const store = mockStore({
|
||||||
config: fromJS({
|
config: fromJS({
|
||||||
media_folder_relative: true,
|
media_folder_relative: true,
|
||||||
@ -41,8 +41,7 @@ describe('mediaLibrary', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// media_folder_relative will be used even if public_folder is specified
|
it('should resolve to relative path and ignore public_folder when media_folder_relative is true', () => {
|
||||||
it('Test relative path resolution, with public folder specified', () => {
|
|
||||||
const store = mockStore({
|
const store = mockStore({
|
||||||
config: fromJS({
|
config: fromJS({
|
||||||
media_folder_relative: true,
|
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({
|
const store = mockStore({
|
||||||
config: fromJS({
|
config: fromJS({
|
||||||
public_folder: '/static/assets/media',
|
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();
|
const store = mockStore();
|
||||||
|
|
||||||
|
expect.assertions(1);
|
||||||
try {
|
try {
|
||||||
store.dispatch(insertMedia({ foo: 'foo.png' }));
|
store.dispatch(insertMedia({ foo: 'foo.png' }));
|
||||||
throw new Error('Expected Exception');
|
|
||||||
} catch (e) {
|
} 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',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -104,8 +104,10 @@ export function insertMedia(media) {
|
|||||||
const publicFolder = config.get('public_folder');
|
const publicFolder = config.get('public_folder');
|
||||||
mediaPath = resolveMediaFilename(media.name, { publicFolder });
|
mediaPath = resolveMediaFilename(media.name, { publicFolder });
|
||||||
}
|
}
|
||||||
|
} else if (Array.isArray(media) || typeof media === 'string') {
|
||||||
|
mediaPath = media;
|
||||||
} else {
|
} 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 } });
|
dispatch({ type: MEDIA_INSERT, payload: { mediaPath } });
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user