Skip to content
Draft
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
6 changes: 4 additions & 2 deletions nx/blocks/loc/views/dashboard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export async function copyProject(project, email) {
// Replace the last path segment (timestamp) with the new timestamp
const newPath = path.substring(0, path.lastIndexOf('/') + 1) + newTimestamp;

await daFetch({ url: `${DA_ADMIN}/source${newPath}.json`, opts: { body, method: 'POST' } });
const resp = await daFetch({ url: `${DA_ADMIN}/source${newPath}.json`, opts: { body, method: 'POST' } });
if (!resp.ok) throw new Error(`Copy failed (${resp.status})`);

// Return just the path and timestamp for the new project
return { path: newPath, lastModified: newTimestamp, newProject };
Expand All @@ -71,6 +72,7 @@ export async function archiveProject(project) {
const newPath = path.replace(TRANSLATION_PATH_ACTIVE, TRANSLATION_PATH_ARCHIVE);
formData.append('destination', `${newPath}.json`);
const opts = { body: formData, method: 'POST' };
await daFetch({ url: `${DA_ADMIN}/move${path}.json`, opts });
const resp = await daFetch({ url: `${DA_ADMIN}/move${path}.json`, opts });
if (!resp.ok) throw new Error(`Archive failed (${resp.status})`);
return newPath;
}
39 changes: 39 additions & 0 deletions test/loc/views/dashboard/copy-archive.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { expect } from '@esm-bundle/chai';
import { copyProject, archiveProject } from '../../../../nx/blocks/loc/views/dashboard/index.js';

const project = { path: '/org/site/.da/translation/active/1' };

describe('dashboard copy and archive', () => {
let origFetch;

const respondWith = (status) => {
window.fetch = async (url, opts) => {
if (opts.method === 'POST') return new Response(null, { status });
return new Response(JSON.stringify({ title: 'Proj', langs: [] }), { status: 200 });
};
};

beforeEach(() => { origFetch = window.fetch; });
afterEach(() => { window.fetch = origFetch; });

it('reports a failed copy as an error', async () => {
respondWith(403);
const error = await copyProject(project, 'me@example.com').catch((e) => e);
expect(error.message).to.equal('Copy failed (403)');
});

it('reports a failed archive as an error', async () => {
respondWith(403);
const error = await archiveProject(project).catch((e) => e);
expect(error.message).to.equal('Archive failed (403)');
});

it('returns the new paths when copy and archive work', async () => {
respondWith(201);
const { path, newProject } = await copyProject(project, 'me@example.com');
expect(path).to.match(/^\/org\/site\/\.da\/translation\/active\/\d+$/);
expect(newProject.title).to.equal('Proj-copy');
respondWith(204);
expect(await archiveProject(project)).to.equal('/org/site/.da/translation/archive/1');
});
});
Loading