Repository: freeCodeCamp/freeCodeCamp
File: e2e/output.spec.ts (TypeScript)

Code before:
import { test, expect, type Page } from '@playwright/test';

import translations from '../client/i18n/locales/english/translations.json';
import { clearEditor, getEditors } from './utils/editor';

const outputTexts = {
  default: `
  /**
  * Your test output will go here
  */`,
  syntaxError: `SyntaxError: unknown: Unexpected token (1:3)

  > 1 | var
      |    ^`,
  empty: `// running tests
  You should declare myName with the var keyword, ending with a semicolon
  // tests completed`,
  passed: `// running tests
// tests completed`
};

interface InsertTextParameters {
  page: Page;
  isMobile: boolean;
  text: string;
}

const insertTextInCodeEditor = async ({
  page,
  isMobile,
  text
}: InsertTextParameters) => {
  if (isMobile) {
    await page
      .getByRole('tab', { name: translations.learn['editor-tabs'].code })
      .click();
  }
  await getEditors(page).fill(text);
  if (isMobile) {
    await page
      .getByRole('tab', { name: translations.learn['editor-tabs'].console })
      .click();
  }
};

const runChallengeTest = async (page: Page, isMobile: boolean) => {
  if (isMobile) {
    await page.getByRole('tab', { name: 'Code' }).click();
    await page.getByText('Run').click();
    await page.getByRole('tab', { name: 'Console' }).click();
  } else {
    await page.getByText('Run the Tests (Ctrl + Enter)').click();
  }
};

test.beforeEach(async ({ page }) => {
  await page.goto(
    '/learn/javascript-algorithms-and-data-structures/basic-javascript/declare-javascript-variables'
  );
});

test.describe('Challenge Output Component Tests', () => {
  test('should render with default output', async ({ page, isMobile }) => {
    if (isMobile) {
      await page.getByRole('tab', { name: 'Console' }).click();
    }
    const outputConsole = page.getByRole('region', {
      name: translations.learn['editor-tabs'].console
    });
    await expect(outputConsole).toBeVisible();
    await expect(outputConsole).toHaveText(outputTexts.default);
  });

  test('should contain syntax error output when var is entered in editor', async ({
    page,
    isMobile
  }) => {
    await insertTextInCodeEditor({ page, isMobile, text: 'var' });
    await expect(
      page.getByRole('region', {
        name: translations.learn['editor-tabs'].console
      })
    ).toHaveText(outputTexts.syntaxError);
  });

  test('should contain reference error output when var is entered in editor', async ({
    page,
    isMobile
  }) => {
    const referenceErrorRegex =
      /ReferenceError: (myName is not defined|Can't find variable: myName)/;
    await insertTextInCodeEditor({ page, isMobile, text: 'myName' });
    await expect(
      page.getByRole('region', {
        name: translations.learn['editor-tabs'].console
      })
    ).toHaveText(referenceErrorRegex);
  });

  test('should contain final output after test fail', async ({
    page,
    isMobile
  }) => {
    await runChallengeTest(page, isMobile);
    await expect(
      page.getByRole('region', {
        name: translations.learn['editor-tabs'].console
      })
    ).toHaveText(outputTexts.empty);
  });

  test('should contain final output after test pass', async ({
    page,
    isMobile
  }) => {
    const closeButton = page.getByRole('button', { name: 'Close' });

    await insertTextInCodeEditor({ page, isMobile, text: 'var myName;' });
    await runChallengeTest(page, isMobile);
    await closeButton.click();
    await expect(
      page.getByRole('region', {
        name: translations.learn['editor-tabs'].console
      })
    ).toHaveText(outputTexts.passed);
  });

  test('jquery challenge should render with default output', async ({
    page
  }) => {
    await page.goto(
      '/learn/front-end-development-libraries/jquery/target-html-elements-with-selectors-using-jquery'
    );
    await expect(
      page.getByRole('region', {
        name: translations.learn['editor-tabs'].console
      })
    ).toHaveText(outputTexts.default);

    await expect(
      page.getByRole('region', {
        name: translations.learn['editor-tabs'].console
      })
    ).not.toHaveText('ReferenceError: $ is not defined');
  });

  test('Custom output for JavaScript Objects set and map', async ({
    page,
    browserName
  }) => {
    await page.goto(
      '/learn/javascript-algorithms-and-data-structures/basic-javascript/comment-your-javascript-code'
    );
    await insertTextInCodeEditor({
      page,
      isMobile: false,
      text: 'const set = new Set(); set.add(1); set.add("set"); set.add(10); console.log(set);'
    });
    await expect(
      page.getByRole('region', {
        name: translations.learn['editor-tabs'].console
      })
    ).toContainText('Set(3) {1, set, 10}');

    await clearEditor({ browserName, page });

    await insertTextInCodeEditor({
      page,
      isMobile: false,
      text: 'const map = new Map(); map.set(1, "one"); map.set("two", 2); console.log(map);'
    });

    await expect(
      page.getByRole('region', {
        name: translations.learn['editor-tabs'].console
      })
    ).toContainText('Map(2) {1 => one, two => 2}');
  });
});


Review comment (suggestion):
```suggestion
    page,
    isMobile,
```
So it can be used below

Code after:
});

test.describe('Jquery challenges', () => {
  test('Jquery challenge should render with default output', async ({
    page
  }) => {
    await page.goto(
      '/learn/front-end-development-libraries/jquery/target-html-elements-with-selectors-using-jquery'
    );
    await expect(
      page.getByRole('region', {
        name: translations.learn['editor-tabs'].console
      })
    ).toHaveText(outputTexts.default);

    await expect(
      page.getByRole('region', {
        name: translations.learn['editor-tabs'].console
      })
    ).not.toHaveText('ReferenceError: $ is not defined');
  });
});

test.describe('Custom output for Set and Map', () => {
  test('Custom output for JavaScript Objects Set and Map', async ({
    page,
    isMobile,
    browserName
  }) => {
    await page.goto(
      '/learn/javascript-algorithms-and-data-structures/basic-javascript/comment-your-javascript-code'
    );
    await insertTextInCodeEditor({
      page,
      isMobile,
      text: 'const set = new Set(); set.add(1); set.add("set"); set.add(10); console.log(set);'
    });
    await expect(
      page.getByRole('region', {
        name: translations.learn['editor-tabs'].console
      })
    ).toContainText('Set(3) {1, set, 10}');

    await clearEditor({ browserName, page });

    await insertTextInCodeEditor({
      page,
      isMobile,
      text: 'const map = new Map(); map.set(1, "one"); map.set("two", 2); console.log(map);'
    });