name: Library Tests

on: pull_request

jobs:
  test-matrix:
    strategy:
      matrix:
        node: [ '12', '14' ]
    name: Test Library Node v ${{ matrix.node }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v2
      - name: Setup NodeJS
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node }}
      - name: Cache node modules
        id: cache-node-modules
        uses: actions/cache@v1
        with:
            path: ./node_modules
            key: test-${{ matrix.node }}-${{hashFiles('./package-lock.json')}}
            restore-keys: test-${{ matrix.node }}-${{hashFiles('./package-lock.json')}}
      - name: Install modules
        if: steps.cache-node-modules.outputs.cache-hit != 'true'
        run: npm install
      - name: Run tests
        run: npm test

  test-success:
    runs-on: ubuntu-latest
    needs: test-matrix
    steps:
      - name: Tests completed
        run: echo Done!

  test-coverage:
    runs-on: ubuntu-latest
    needs: test-success
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          node-version: 12
      - uses: actions/cache@v1
        id: cache-node-modules
        with:
          path: ./node_modules
          key: test-12-${{hashFiles('./package-lock.json')}}
          restore-keys: test-12-${{hashFiles('./package-lock.json')}}
      - name: Install modules
        if: steps.cache-node-modules.outputs.cache-hit != 'true'
        run: npm install
      - name: Test with coverage
        run: npm run test-cover
      - name: Coveralls
        uses: coverallsapp/github-action@master
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
