# Run tests, build components bundle, and deploy it to sandbox

name: 'JS API Clients test'

on:
  pull_request:
    paths:
      - 'react-api-client/**'
      - 'api-client/**'
      - 'package.json'
      - '.github/workflows/react-api-client-test.yaml'
  push:
    paths:
      - 'react-api-client/**'
      - 'api-client/**'
      - 'package.json'
      - '.github/workflows/react-api-client-test.yaml'
    branches:
      - '*'
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}-${{ github.ref_name != 'edge' || github.run_id}}-${{ github.ref_type != 'tag' || github.run_id }}
  cancel-in-progress: true

defaults:
  run:
    shell: bash

env:
  CI: true

jobs:
  js-unit-test:
    name: 'api-client and react-api-client unit tests'
    timeout-minutes: 30
    runs-on: 'ubuntu-22.04'
    steps:
      - uses: 'actions/checkout@v3'
      - uses: 'actions/setup-node@v3'
        with:
          node-version: '18.19.0'
      - name: 'install libudev for usb-detection'
        run: |
          # WORKAROUND: Remove microsoft debian repo due to https://github.com/microsoft/linux-package-repositories/issues/130. Remove line below after it is resolved
          sudo rm -f /etc/apt/sources.list.d/microsoft-prod.list
          sudo apt-get update && sudo apt-get install libudev-dev
      - name: 'cache yarn cache'
        uses: actions/cache@v3
        with:
          path: |
            ${{ github.workspace }}/.yarn-cache
            ${{ github.workspace }}/.npm-cache
          key: js-${{ secrets.GH_CACHE_VERSION }}-${{ runner.os }}-yarn-${{ hashFiles('yarn.lock') }}
          restore-keys: |
            js-${{ secrets.GH_CACHE_VERSION }}-${{ runner.os }}-yarn-
      - name: 'setup-js'
        run: |
          npm config set cache ./.npm-cache
          yarn config set cache-folder ./.yarn-cache
          make setup-js
      - name: 'run api-client and react-api-client unit tests'
        run: make -C api-client test-cov
      - name: 'Upload coverage report'
        uses: codecov/codecov-action@v3
        with:
          files: ./coverage/lcov.info
          flags: react-api-client