DEV Community

Rahul
Rahul

Posted on

Testcraft Studio frontend 41: src/suiteExport.js

import { csv } from './store.jsx';
import { defaultFields } from './seed.js';

export function suiteExport(state, workspace, suite) {
  if (!suite?.tests?.length || suite.workspaceId !== workspace.id) throw new Error('Choose a test suite in this workspace.');
  const template = suite.templateSnapshot || state.templates.find(item => item.id === suite.templateId);
  const fields = template?.fields || defaultFields;
  const value = (test, field) => {
    const key = field.toLowerCase().replace(/[^a-z0-9]/g, '');
    const evidence = test.sourceIds.map(id => {
      const doc = suite.sourceSnapshot?.find(item => item.id === id) || state.documents.find(item => item.id === id);
      return doc ? `${doc.name} · revision ${doc.revision}${doc.locator ? ` · ${doc.locator}` : ''}` : 'Source unavailable';
    }).join('; ');
    const values = {
      testid: test.id, testcaseid: test.id, title: test.title, testcasetitle: test.title,
      ownerteam: workspace.team, type: test.type || suite.kind || '', testtype: test.type || suite.kind || '',
      category: test.category, requirement: test.requirement, requirementid: test.requirement,
      objective: test.objective, preconditions: test.preconditions, testdata: test.data,
      steps: test.steps.map((step, index) => `${index + 1}. ${step}`).join('\n'), teststeps: test.steps.join('\n'),
      expectedresult: test.expected, expectedresults: test.expected, priority: test.priority,
      evidence, source: evidence, sources: evidence, reviewstatus: test.status, notes: test.notes, reviewnotes: test.notes,
    };
    return values[key] ?? test.customFields?.[field] ?? '';
  };
  return {
    filename: `${workspace.ref}-tests-context-${suite.version}.csv`,
    mimeType: 'text/csv;charset=utf-8',
    content: csv([fields, ...suite.tests.map(test => fields.map(field => value(test, field)))]),
  };
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)