name: Checks
on:
  pull_request:
  push:
    branches:
      - main
      
jobs:
  lint:
    name: ESLint
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v3

      - name: Setup node
        uses: actions/setup-node@v3
        with:
          node-version: 18
          cache: 'yarn'

      - name: Install dependencies
        uses: bahmutov/npm-install@v1
        with:
          useRollingCache: true

      - name: Run ESLint
        run: yarn lint:eslint

  type_check:
    name: TypeScript
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v3

      - name: Setup node
        uses: actions/setup-node@v3
        with:
          node-version: 18
          cache: 'yarn'

      - name: Install dependencies
        uses: bahmutov/npm-install@v1
        with:
          useRollingCache: true

      - name: Compile TypeScript
        run: yarn lint:tsc

  jest_tests:
    name: Run unit tests with Jest
    needs: [ lint, type_check ]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v3

      - name: Setup node
        uses: actions/setup-node@v3
        with:
          node-version: 18
          cache: 'yarn'

      - name: Install dependencies
        uses: bahmutov/npm-install@v1
        with:
          useRollingCache: true

      - name: Run Jest
        run: yarn test:jest

  pw_tests:
    name: Run components visual tests with PlayWright
    needs: [ lint, type_check ]
    runs-on: ubuntu-latest
    container:
      image: mcr.microsoft.com/playwright:v1.28.0-focal

    steps:
      - name: Install git-lfs
        run: apt-get update && apt-get install git-lfs

      - name: Checkout repo
        uses: actions/checkout@v3
        with:
          lfs: 'true'

      - name: Setup node
        uses: actions/setup-node@v3
        with:
          node-version: 18
          cache: 'yarn'

      - name: Install dependencies
        uses: bahmutov/npm-install@v1
        with:
          useRollingCache: true

      - name: Run PlayWright
        run: HOME=/root yarn test:pw

      - name: Upload test results
        if: always()
        uses: actions/upload-artifact@v2
        with:
          name: playwright-report
          path: playwright-report
          retention-days: 10