Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions app/utils/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,6 @@ export default class ProjectUtil {
* @param {array} assetGroups Array of asset group objects
* @returns A modified copy of assetGroups that has the paths converted from absolute to relative
*/
// TODO - unit tests!!
static absoluteToRelativePathForAssetGroups(projectPath, assetGroups) {
// If the array of asset groups isn't defined, we will return an empty array just to start
// initializing the collection.
Expand All @@ -613,7 +612,6 @@ export default class ProjectUtil {
* @param {array} assetGroups Array of asset group objects
* @returns A modified copy of assetGroups that has the paths converted from relative to absolute
*/
// TODO - unit tests!!
static relativeToAbsolutePathForAssetGroups(projectPath, assetGroups) {
// If the array of asset groups isn't defined, we will return an empty array just to start
// initializing the collection.
Expand Down
69 changes: 69 additions & 0 deletions test/utils/project.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { v4 as uuid } from 'uuid';
import ProjectUtil from '../../app/utils/project';
import Constants from '../../app/constants/constants';
import WorkflowUtil from '../../app/utils/workflow';
import AssetUtil from '../../app/utils/asset';

jest.mock('uuid');

Expand Down Expand Up @@ -1548,4 +1549,72 @@ describe('utils', () => {
expect(project.externalAssets.children.length).toEqual(1);
});
});

describe('absoluteToRelativePathForAssetGroups', () => {
it('should return an empty array if assetGroups is null', () => {
expect(ProjectUtil.absoluteToRelativePathForAssetGroups('/project', null)).toEqual([]);
});

it('should return an empty array if assetGroups is undefined', () => {
expect(ProjectUtil.absoluteToRelativePathForAssetGroups('/project', undefined)).toEqual([]);
});

it('should convert absolute paths to relative paths for each asset group', () => {
const isWin = process.platform === 'win32';
const projectPath = isWin ? 'C:\\project' : '/project';

const file1Path = isWin ? 'C:\\project\\file1' : '/project/file1';
const file2Path = isWin ? 'C:\\project\\dir\\file2' : '/project/dir/file2';
const file3Path = isWin ? 'C:\\project\\file3' : '/project/file3';

const testAssetGroups = [
{ name: 'Group 1', assets: [{ uri: file1Path, type: Constants.AssetType.FILE }, { uri: file2Path, type: Constants.AssetType.FILE }] },
{ name: 'Group 2', assets: [{ uri: file3Path, type: Constants.AssetType.FILE }] },
];

// Clone the test groups carefully to avoid in-place modification issues
const inputGroups = JSON.parse(JSON.stringify(testAssetGroups));
const result = ProjectUtil.absoluteToRelativePathForAssetGroups(projectPath, inputGroups);

// The method normalizes separators to POSIX internally
const expectedFile2Relative = 'dir/file2';

// Assert the realistic outcome
expect(result[0].assets).toEqual([{ uri: 'file1', type: Constants.AssetType.FILE }, { uri: expectedFile2Relative, type: Constants.AssetType.FILE }]);
expect(result[1].assets).toEqual([{ uri: 'file3', type: Constants.AssetType.FILE }]);
});
});

describe('relativeToAbsolutePathForAssetGroups', () => {
it('should return an empty array if assetGroups is null', () => {
expect(ProjectUtil.relativeToAbsolutePathForAssetGroups('/project', null)).toEqual([]);
});

it('should return an empty array if assetGroups is undefined', () => {
expect(ProjectUtil.relativeToAbsolutePathForAssetGroups('/project', undefined)).toEqual([]);
});

it('should convert relative paths to absolute paths for each asset group', () => {
const isWin = process.platform === 'win32';
const projectPath = isWin ? 'C:\\project' : '/project';

const expectedFile1 = isWin ? 'C:\\project\\file1' : '/project/file1';
const expectedFile2 = isWin ? 'C:\\project\\dir\\file2' : '/project/dir/file2';
const expectedFile3 = isWin ? 'C:\\project\\file3' : '/project/file3';

const relativeFile2 = 'dir/file2';

const testAssetGroups = [
{ name: 'Group 1', assets: [{ uri: 'file1', type: Constants.AssetType.FILE }, { uri: relativeFile2, type: Constants.AssetType.FILE }] },
{ name: 'Group 2', assets: [{ uri: 'file3', type: Constants.AssetType.FILE }] },
];

const inputGroups = JSON.parse(JSON.stringify(testAssetGroups));
const result = ProjectUtil.relativeToAbsolutePathForAssetGroups(projectPath, inputGroups);

// Assert the realistic outcome
expect(result[0].assets).toEqual([{ uri: expectedFile1, type: Constants.AssetType.FILE }, { uri: expectedFile2, type: Constants.AssetType.FILE }]);
expect(result[1].assets).toEqual([{ uri: expectedFile3, type: Constants.AssetType.FILE }]);
});
});
});