# This is a basic workflow to help you get started with Actions

name: test

on:
  push:
    branches: [ main, release, test-code-coverage ]
  pull_request:
    branches: [ main, release ]

  workflow_dispatch:

jobs:
  jest:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: '14'
      - name: Run install
        run: npm i -g lerna && npm run bootstrap
      - name: Run Jest test
        run: npm run test:coverage
      - name: Archive Jest coverage
        uses: actions/upload-artifact@v3
        with:
          if-no-files-found: error
          name: jest
          path: test/coverage/coverage-final.json
  cypress:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: '14'
      - name: Run install
        run: |
          npm i -g lerna
          npm run bootstrap
      - name: Build storybook
        run: |
          npm run pre-story
          TEST_ENV=test npm run build-storybook
      - name: Serve storybook
        run: nohup npx http-server -p 6006 storybook-static &
      - name: Run Cypress test
        run: npx wait-on http://127.0.0.1:6006 && ./node_modules/.bin/cypress run
      - name: Archive Cypress coverage
        uses: actions/upload-artifact@v3
        with:
          if-no-files-found: error
          name: cypress
          path: cypress/coverage/coverage-final.json
  coverage:
    runs-on: ubuntu-latest
    needs: [jest, cypress]
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: '14'
      - name: Download Jest coverage
        uses: actions/download-artifact@v3
        with:
          # upload jest and cypress coverage to output dir
          path: output
      - name: Code coverage merge
        run: |
          tree output
          npx istanbul-merge --out output/coverage-final.json output/jest/coverage-final.json output/cypress/coverage-final.json
          tree output
      - name: Run codecov
        run: npx codecov --token=${{ secrets.CODECOV_TOKEN }} --file=output/coverage-final.json
