version: 2.0

jobs:
  build:
    docker:
      - image: circleci/node:12.18.3
    working_directory: ~/react-plotly.js
    steps:
      - checkout
      - restore_cache:
          keys:
            - v{{ .Environment.CIRCLE_CACHE_VERSION }}-deps-{{ .Branch }}-{{ checksum "package.json" }}
            - v{{ .Environment.CIRCLE_CACHE_VERSION }}-deps-master-{{ checksum "package.json" }}
      - run:
          name: Install dependencies
          command: |
            npm install
      - run:
          name: List dependency versions
          command: |
            echo "npm: $(npm --version)"
            echo "node: $(node --version)"
            npm ls || true
      - save_cache:
          paths:
            - node_modules
          key: v{{ .Environment.CIRCLE_CACHE_VERSION }}-deps-{{ .Branch }}-{{ checksum "package.json" }}
      - persist_to_workspace:
          root: .
          paths:
            - node_modules

  test:
    docker:
      - image: circleci/node:12.18.3
    working_directory: ~/react-plotly.js
    steps:
      - checkout
      - attach_workspace:
          at: ~/react-plotly.js
      - run:
          name: Run tests
          command: npm run test

workflows:
  version: 2
  build-and-test:
    jobs:
      - build
      - test:
          requires:
            - build
