2019-02-08 22:50:39 -05:00
|
|
|
import { queryHelpers, waitForElement } from 'dom-testing-library';
|
2018-12-11 11:30:30 -05:00
|
|
|
import uuid from 'uuid/v4';
|
2019-02-08 22:50:39 -05:00
|
|
|
import uploadcare from '../index';
|
2018-12-11 11:30:30 -05:00
|
|
|
|
|
|
|
function generateMockUrl({ count = 1, cdnUrl } = {}) {
|
|
|
|
const baseUrl = 'https://ucarecdn.com';
|
|
|
|
const url = `${baseUrl}/${uuid()}~${count}/`;
|
|
|
|
const result =
|
|
|
|
count === 1 ? `${url}nth/0/` : Array.from({ length: count }, (val, idx) => `${url}nth/${idx}/`);
|
|
|
|
if (cdnUrl) {
|
|
|
|
return { result, cdnUrl: url };
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('uploadcare media library', () => {
|
|
|
|
let handleInsert;
|
|
|
|
let simulateCloseDialog;
|
2019-02-08 22:50:39 -05:00
|
|
|
let uploadcareScripts = [];
|
2018-12-11 11:30:30 -05:00
|
|
|
const TEST_PUBLIC_KEY = 123;
|
|
|
|
const defaultConfig = {
|
|
|
|
imagesOnly: false,
|
|
|
|
multiple: false,
|
|
|
|
previewStep: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2019-02-08 22:50:39 -05:00
|
|
|
/**
|
|
|
|
* We load the Uploadcare library by injecting script tags to the page
|
|
|
|
* head. Initialization waits for the scripts to load, so here we fake it.
|
|
|
|
* This also tests that the expected scripts are added to the DOM.
|
|
|
|
*/
|
|
|
|
[/uploadcare\.full\.js$/, /uploadcare\.tab-effects\.js$/].forEach(pattern => {
|
|
|
|
waitForElement(() => {
|
|
|
|
return queryHelpers.queryByAttribute('src', document, pattern);
|
|
|
|
}).then(script => {
|
|
|
|
uploadcareScripts.push(script);
|
|
|
|
script.onreadystatechange();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
let openDialogCallback;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mock of the uploadcare widget object itself.
|
|
|
|
*/
|
|
|
|
window.uploadcare = {
|
|
|
|
registerTab: jest.fn(),
|
|
|
|
openDialog: jest.fn(() => ({
|
|
|
|
done: jest.fn(cb => {
|
|
|
|
openDialogCallback = cb;
|
|
|
|
}),
|
|
|
|
})),
|
|
|
|
fileFrom: jest.fn((type, url) =>
|
|
|
|
Promise.resolve({
|
|
|
|
testFileUrl: url,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
loadFileGroup: () => ({
|
|
|
|
done: cb => cb(),
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
2018-12-11 11:30:30 -05:00
|
|
|
/**
|
|
|
|
* Mock to manually call the close dialog registered callback.
|
|
|
|
*/
|
|
|
|
simulateCloseDialog = result =>
|
|
|
|
openDialogCallback({
|
|
|
|
promise: () => Promise.resolve(result),
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Spy to serve as the Netlify CMS insertion handler.
|
|
|
|
*/
|
|
|
|
handleInsert = jest.fn();
|
|
|
|
});
|
|
|
|
|
2019-02-08 22:50:39 -05:00
|
|
|
afterEach(() => {
|
|
|
|
/**
|
|
|
|
* Remove the script elements from the dom after each test.
|
|
|
|
*/
|
|
|
|
const { head } = document;
|
|
|
|
uploadcareScripts.forEach(script => head.contains(script) && head.removeChild(script));
|
|
|
|
uploadcareScripts = [];
|
|
|
|
});
|
|
|
|
|
2018-12-11 11:30:30 -05:00
|
|
|
it('exports an object with expected properties', () => {
|
2019-02-08 22:50:39 -05:00
|
|
|
expect(uploadcare).toMatchInlineSnapshot(`
|
2018-12-11 11:30:30 -05:00
|
|
|
Object {
|
|
|
|
"init": [Function],
|
|
|
|
"name": "uploadcare",
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('initialization', () => {
|
|
|
|
it('sets global required configuration', async () => {
|
|
|
|
const options = {
|
|
|
|
config: {
|
|
|
|
publicKey: TEST_PUBLIC_KEY,
|
|
|
|
},
|
|
|
|
};
|
2019-02-08 22:50:39 -05:00
|
|
|
await uploadcare.init({ options });
|
2018-12-11 11:30:30 -05:00
|
|
|
expect(window.UPLOADCARE_LIVE).toEqual(false);
|
|
|
|
expect(window.UPLOADCARE_MANUAL_START).toEqual(true);
|
|
|
|
expect(window.UPLOADCARE_PUBLIC_KEY).toEqual(TEST_PUBLIC_KEY);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('registers the effects tab', async () => {
|
2019-02-08 22:50:39 -05:00
|
|
|
const mockEffectsTab = { mockEffectsTab: true };
|
|
|
|
window.uploadcareTabEffects = mockEffectsTab;
|
|
|
|
await uploadcare.init();
|
|
|
|
expect(window.uploadcare.registerTab).toHaveBeenCalledWith('preview', mockEffectsTab);
|
2018-12-11 11:30:30 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('configuration', () => {
|
|
|
|
const options = {
|
|
|
|
config: {
|
|
|
|
foo: 'bar',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
it('has defaults', async () => {
|
2019-02-08 22:50:39 -05:00
|
|
|
const integration = await uploadcare.init();
|
2018-12-11 11:30:30 -05:00
|
|
|
await integration.show();
|
2019-02-08 22:50:39 -05:00
|
|
|
expect(window.uploadcare.openDialog).toHaveBeenCalledWith(null, defaultConfig);
|
2018-12-11 11:30:30 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can be defined globally', async () => {
|
|
|
|
const expectedConfig = {
|
|
|
|
...defaultConfig,
|
|
|
|
...options.config,
|
|
|
|
};
|
2019-02-08 22:50:39 -05:00
|
|
|
const integration = await uploadcare.init({ options });
|
2018-12-11 11:30:30 -05:00
|
|
|
await integration.show();
|
2019-02-08 22:50:39 -05:00
|
|
|
expect(window.uploadcare.openDialog).toHaveBeenCalledWith(null, expectedConfig);
|
2018-12-11 11:30:30 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can be defined per field', async () => {
|
|
|
|
const expectedConfig = {
|
|
|
|
...defaultConfig,
|
|
|
|
...options.config,
|
|
|
|
};
|
2019-02-08 22:50:39 -05:00
|
|
|
const integration = await uploadcare.init();
|
2018-12-11 11:30:30 -05:00
|
|
|
await integration.show({ config: options.config });
|
2019-02-08 22:50:39 -05:00
|
|
|
expect(window.uploadcare.openDialog).toHaveBeenCalledWith(null, expectedConfig);
|
2018-12-11 11:30:30 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('show method', () => {
|
|
|
|
const options = {
|
|
|
|
config: {
|
|
|
|
foo: 'bar',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
it('accepts imagesOnly as standalone property', async () => {
|
|
|
|
const expectedConfig = {
|
|
|
|
...defaultConfig,
|
|
|
|
...options.config,
|
|
|
|
imagesOnly: true,
|
|
|
|
};
|
2019-02-08 22:50:39 -05:00
|
|
|
const integration = await uploadcare.init();
|
2018-12-11 11:30:30 -05:00
|
|
|
await integration.show({ config: options.config, imagesOnly: true });
|
2019-02-08 22:50:39 -05:00
|
|
|
expect(window.uploadcare.openDialog).toHaveBeenCalledWith(null, expectedConfig);
|
2018-12-11 11:30:30 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('allows multiple selection if allowMultiple is not false', async () => {
|
|
|
|
options.config.multiple = true;
|
|
|
|
const expectedConfig = {
|
|
|
|
...defaultConfig,
|
|
|
|
...options.config,
|
|
|
|
multiple: true,
|
|
|
|
};
|
2019-02-08 22:50:39 -05:00
|
|
|
const integration = await uploadcare.init({ options });
|
2018-12-11 11:30:30 -05:00
|
|
|
await integration.show({ config: options.config });
|
2019-02-08 22:50:39 -05:00
|
|
|
expect(window.uploadcare.openDialog).toHaveBeenCalledWith(null, expectedConfig);
|
2018-12-11 11:30:30 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('disallows multiple selection if allowMultiple is false', async () => {
|
|
|
|
options.config.multiple = true;
|
|
|
|
const expectedConfig = {
|
|
|
|
...defaultConfig,
|
|
|
|
...options.config,
|
|
|
|
multiple: false,
|
|
|
|
};
|
2019-02-08 22:50:39 -05:00
|
|
|
const integration = await uploadcare.init({ options });
|
2018-12-11 11:30:30 -05:00
|
|
|
await integration.show({ config: options.config, allowMultiple: false });
|
2019-02-08 22:50:39 -05:00
|
|
|
expect(window.uploadcare.openDialog).toHaveBeenCalledWith(null, expectedConfig);
|
2018-12-11 11:30:30 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('passes selected image url to handleInsert', async () => {
|
|
|
|
const url = generateMockUrl();
|
|
|
|
const mockResult = { cdnUrl: url };
|
2019-02-08 22:50:39 -05:00
|
|
|
const integration = await uploadcare.init({ handleInsert });
|
2018-12-11 11:30:30 -05:00
|
|
|
await integration.show();
|
|
|
|
await simulateCloseDialog(mockResult);
|
|
|
|
expect(handleInsert).toHaveBeenCalledWith(url);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passes multiple selected image urls to handleInsert', async () => {
|
|
|
|
options.config.multiple = true;
|
|
|
|
const { result, cdnUrl } = generateMockUrl({ count: 3, cdnUrl: true });
|
|
|
|
const mockDialogCloseResult = { cdnUrl, count: 3 };
|
2019-02-08 22:50:39 -05:00
|
|
|
const integration = await uploadcare.init({ options, handleInsert });
|
2018-12-11 11:30:30 -05:00
|
|
|
await integration.show();
|
|
|
|
await simulateCloseDialog(mockDialogCloseResult);
|
|
|
|
expect(handleInsert).toHaveBeenCalledWith(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('enableStandalone method', () => {
|
|
|
|
it('returns false', async () => {
|
2019-02-08 22:50:39 -05:00
|
|
|
const integration = await uploadcare.init();
|
2018-12-11 11:30:30 -05:00
|
|
|
expect(integration.enableStandalone()).toEqual(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|