Files
Sam Lanning a47a29fb8b Allow for relative paths of media files (#2394)
* Allow for relative paths of media files

fixes #325

* Switch to calculating the relative path based on collection

The required relative path is now calculated depending on the
location of the collection of the current entry having the
media inserted into. And the configuration option has now been
changed to a boolean flag.

This allows collections to not neccesarily all be in the same
location relative to the media folder, and simplifies config.

* Clean up code and fix linting

* Add unit tests to resolveMediaFilename()

* Rework insertMedia action to fetch own config

This moves more of the media path resolution logic into the action
which makes it easier to unit test

* Add unit tests for the mediaLibrary.insertMedia action

* yarn run format

* add dependabot config (#2580)
2019-08-24 16:03:09 -04:00

143 lines
4.1 KiB
JavaScript

import { resolveMediaFilename, fileExtensionWithSeparator, fileExtension } from '../path';
describe('resolveMediaFilename', () => {
it('publicly Accessible URL, no slash', () => {
expect(
resolveMediaFilename('image.png', {
publicFolder: 'static/assets',
}),
).toEqual('/static/assets/image.png');
});
it('publicly Accessible URL, with slash', () => {
expect(
resolveMediaFilename('image.png', {
publicFolder: '/static/assets',
}),
).toEqual('/static/assets/image.png');
});
it('publicly Accessible URL, root', () => {
expect(
resolveMediaFilename('image.png', {
publicFolder: '/',
}),
).toEqual('/image.png');
});
it('relative URL, same folder', () => {
expect(
resolveMediaFilename('image.png', {
mediaFolder: '/content/posts',
collectionFolder: '/content/posts',
}),
).toEqual('image.png');
});
it('relative URL, same folder, with slash', () => {
expect(
resolveMediaFilename('image.png', {
mediaFolder: '/content/posts/',
collectionFolder: '/content/posts',
}),
).toEqual('image.png');
});
it('relative URL, same folder, with slashes', () => {
expect(
resolveMediaFilename('image.png', {
mediaFolder: '/content/posts/',
collectionFolder: '/content/posts/',
}),
).toEqual('image.png');
});
it('relative URL, sibling folder', () => {
expect(
resolveMediaFilename('image.png', {
mediaFolder: '/content/images/',
collectionFolder: '/content/posts/',
}),
).toEqual('../images/image.png');
});
it('relative URL, cousin folder', () => {
expect(
resolveMediaFilename('image.png', {
mediaFolder: '/content/images/pngs/',
collectionFolder: '/content/markdown/posts/',
}),
).toEqual('../../images/pngs/image.png');
});
it('relative URL, parent folder', () => {
expect(
resolveMediaFilename('image.png', {
mediaFolder: '/content/',
collectionFolder: '/content/posts',
}),
).toEqual('../image.png');
});
it('relative URL, child folder', () => {
expect(
resolveMediaFilename('image.png', {
mediaFolder: '/content/images',
collectionFolder: '/content/',
}),
).toEqual('images/image.png');
});
});
describe('fileExtensionWithSeparator', () => {
it('should return the extension of a file', () => {
expect(fileExtensionWithSeparator('index.html')).toEqual('.html');
});
it('should return the extension of a file path', () => {
expect(fileExtensionWithSeparator('/src/main/index.html')).toEqual('.html');
});
it('should return the extension of a file path with trailing slash', () => {
expect(fileExtensionWithSeparator('/src/main/index.html/')).toEqual('.html');
});
it('should return the extension for an extension with two ..', () => {
expect(fileExtensionWithSeparator('/src/main/index..html')).toEqual('.html');
});
it('should return an empty string for the parent path ..', () => {
expect(fileExtensionWithSeparator('..')).toEqual('');
});
it('should return an empty string if the file has no extension', () => {
expect(fileExtensionWithSeparator('/src/main/index')).toEqual('');
});
});
describe('fileExtension', () => {
it('should return the extension of a file', () => {
expect(fileExtension('index.html')).toEqual('html');
});
it('should return the extension of a file path', () => {
expect(fileExtension('/src/main/index.html')).toEqual('html');
});
it('should return the extension of a file path with trailing slash', () => {
expect(fileExtension('/src/main/index.html/')).toEqual('html');
});
it('should return the extension for an extension with two ..', () => {
expect(fileExtension('/src/main/index..html')).toEqual('html');
});
it('should return an empty string for the parent path ..', () => {
expect(fileExtension('..')).toEqual('');
});
it('should return an empty string if the file has no extension', () => {
expect(fileExtension('/src/main/index')).toEqual('');
});
});