Skip to content

Commit

Permalink
test: add unit tests for Body, NextCloudVideoView, NextCloudEmbed - r…
Browse files Browse the repository at this point in the history
…efs #253277
  • Loading branch information
ana-oprea committed Jun 12, 2023
1 parent db00e0c commit e21c9dd
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/NextCloud/Body.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import React from 'react';
import renderer from 'react-test-renderer';
import Body from './Body';
import { isInternalURL } from '@plone/volto/helpers';
import { getFieldURL } from '@eeacms/volto-nextcloud-video-block/helpers';

jest.mock('@eeacms/volto-nextcloud-video-block/helpers', () => ({
getFieldURL: jest.fn(),
}));

jest.mock('@plone/volto/helpers', () => ({
isInternalURL: jest.fn(),
flattenToAppURL: jest.fn(),
withBlockExtensions: jest.fn((Component) => Component),
}));

jest.mock('./players', () => ({
nextCloud: jest.fn(() => <div>NextCloud Player</div>),
}));

describe('Body', () => {
it('renders correctly', () => {
const props = {
data: {
url: 'nextCloud',
align: 'full',
preview_image: '/path/to/image',
},
};

const component = renderer.create(<Body {...props} />);
const json = component.toJSON();
expect(json).toMatchSnapshot();
});

it('renders the correct player based on the url', () => {
const props = {
data: {
url: 'nextCloud',
align: 'full',
preview_image: '/path/to/image',
},
};

isInternalURL.mockReturnValue(true);
getFieldURL.mockReturnValue('/path/to/video');

const component = renderer.create(<Body {...props} />);
expect(component.toJSON().props.className).toContain('video-inner');
});

it('renders the correct player based on the url', () => {
const props = {
data: {
url: 'nextCloud',
align: 'full',
preview_image: '/path/to/image',
},
};

isInternalURL.mockReturnValue(false);
getFieldURL.mockReturnValue('nextCloud');

const component = renderer.create(<Body {...props} />);

expect(component.toJSON().props.className).toContain('video-inner');
});
});
17 changes: 17 additions & 0 deletions src/NextCloud/NextCloudVideoView.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';
import { render } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import View from './NextCloudVideoView';

jest.mock('@plone/volto/helpers', () => ({
withBlockExtensions: jest.fn((Component) => Component),
}));

describe('View', () => {
it('renders the video block', () => {
const data = { align: 'center' };
const { container } = render(<View data={data} className="test-class" />);

expect(container).toBeInTheDocument();
});
});
3 changes: 3 additions & 0 deletions src/NextCloud/__snapshots__/Body.test.jsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Body renders correctly 1`] = `null`;
153 changes: 153 additions & 0 deletions src/NextCloud/players/NextCloudEmbed.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import React from 'react';
import { render } from '@testing-library/react';
import '@testing-library/jest-dom';
import NextCloudEmbed from './NextCloudEmbed';
import { isInternalURL, flattenToAppURL } from '@plone/volto/helpers';
import config from '@plone/volto/registry';

config.settings = {
eea: {
languages: [{ code: 'en', name: 'English' }],
},
};
jest.mock('@plone/volto/helpers', () => ({
isInternalURL: jest.fn(),
flattenToAppURL: jest.fn(),
}));

describe('NextCloudEmbed', () => {
beforeEach(() => {
isInternalURL.mockClear();
flattenToAppURL.mockClear();
});

it('renders without crashing', () => {
const embedSettings = {
placeholder: 'Placeholder',
};
const data = {
url: 'some/url',
autoPlay: false,
loop: false,
};
const { container } = render(
<NextCloudEmbed data={data} embedSettings={embedSettings} />,
);
expect(container.querySelector('video')).toBeInTheDocument();
});

it('should apply the correct src for internal url', () => {
const embedSettings = {
placeholder: 'Placeholder',
};
const data = {
url: 'internal/url',
autoPlay: false,
loop: false,
};

isInternalURL.mockReturnValueOnce(true);
flattenToAppURL.mockReturnValueOnce('flattened/url');

const { container } = render(
<NextCloudEmbed data={data} embedSettings={embedSettings} />,
);
expect(container.querySelector('video')).toHaveAttribute(
'src',
'flattened/url/@@download/file',
);
});

it('should apply the correct src for internal url with @@download', () => {
const embedSettings = {
placeholder: 'Placeholder',
};
const data = {
url: 'internal/url/@@download',
autoPlay: false,
loop: false,
};

isInternalURL.mockReturnValueOnce(true);
flattenToAppURL.mockReturnValueOnce('flattened/url');

const { container } = render(
<NextCloudEmbed data={data} embedSettings={embedSettings} />,
);
expect(container.querySelector('video')).toHaveAttribute(
'src',
'flattened/url',
);
});

it('should apply the correct src for external url and no file path for subtitles', () => {
const data = {
url: 'external/url',
autoPlay: false,
loop: false,
subtitles: [{ file: undefined, language: 'en' }],
};
const embedSettings = {
placeholder: 'Placeholder',
};

isInternalURL.mockReturnValueOnce(false);

const { container } = render(
<NextCloudEmbed data={data} embedSettings={embedSettings} />,
);
expect(container.querySelector('video')).toBeInTheDocument();
expect(container.querySelector('video')).toHaveAttribute(
'src',
'external/url/download',
);
});

it('should apply the correct src for external url and subtitles and the file url is external', () => {
const data = {
url: 'external/url',
autoPlay: false,
loop: false,
subtitles: [{ file: 'external/url/subtitle', language: 'en' }],
};
const embedSettings = {
placeholder: 'Placeholder',
};

isInternalURL.mockReturnValueOnce(false);

const { container } = render(
<NextCloudEmbed data={data} embedSettings={embedSettings} />,
);

expect(container.querySelector('video')).toBeInTheDocument();
expect(container.querySelector('video')).toHaveAttribute(
'src',
'external/url/download',
);
});

it('should apply the correct src for external url and subtitles and the file url is internal', () => {
const data = {
url: 'external/url',
autoPlay: false,
loop: false,
subtitles: [{ file: 'internal/url/subtitle', language: 'en' }],
};
const embedSettings = {
placeholder: 'Placeholder',
};

isInternalURL.mockReturnValueOnce(true);
flattenToAppURL.mockReturnValueOnce('flattened/url');

const { container } = render(
<NextCloudEmbed data={data} embedSettings={embedSettings} />,
);
expect(container.querySelector('video')).toBeInTheDocument();
expect(container.querySelector('video')).toHaveAttribute(
'src',
'flattened/url/@@download/file',
);
});
});

0 comments on commit e21c9dd

Please sign in to comment.