Skip to content
This repository has been archived by the owner on Apr 27, 2023. It is now read-only.

Commit

Permalink
tests: Add some basic tests with Jest (#5)
Browse files Browse the repository at this point in the history
* tests: Add some basic tests with Jest

Check the methods are called with the correct params

* chore: Remove redundant build from CircleCI config

Install deps if no cache before testing
  • Loading branch information
ynnoj committed Aug 31, 2018
1 parent fb2f85b commit c6b807d
Show file tree
Hide file tree
Showing 7 changed files with 2,119 additions and 203 deletions.
3 changes: 3 additions & 0 deletions .babelrc
@@ -0,0 +1,3 @@
{
"presets": ["env"]
}
18 changes: 5 additions & 13 deletions .circleci/config.yml
@@ -1,28 +1,20 @@
version: 2
jobs:
build:
test:
docker:
- image: 'circleci/node:latest'
steps:
- checkout
- restore_cache:
keys:
- nautic-{{ checksum "package.json" }}
# fallback to using the latest cache if no exact match is found
- nautic-
- run: yarn
- nautic-{{ checksum "package.json" }}
# fallback to using the latest cache if no exact match is found
- nautic-
- run: yarn install
- save_cache:
paths:
- node_modules
key: nautic-{{ checksum "package.json" }}
test:
docker:
- image: 'circleci/node:latest'
steps:
- checkout
- restore_cache:
keys:
- nautic-{{ checksum "package.json" }}
- run: yarn test
release:
docker:
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
@@ -0,0 +1 @@
.babelrc
7 changes: 7 additions & 0 deletions package.json
Expand Up @@ -3,6 +3,9 @@
"version": "1.0.0",
"description": "Node.js library for the Mautic REST API",
"main": "index.js",
"scripts": {
"test": "jest"
},
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/ynnoj/nautic.git"
Expand All @@ -14,7 +17,11 @@
"request-promise": "^4.2.2"
},
"devDependencies": {
"babel-core": "^6.26.3",
"babel-jest": "^23.0.1",
"babel-preset-env": "^1.7.0",
"husky": "^1.0.0-rc.8",
"jest": "^23.1.0",
"prettier": "^1.13.5",
"pretty-quick": "^1.6.0",
"semantic-release": "^15.9.12"
Expand Down
23 changes: 23 additions & 0 deletions tests/nautic-consumer.js
@@ -0,0 +1,23 @@
import Nautic from '../index'

export default class NauticConsumer {
constructor() {
this.nautic = new Nautic()
}

createNewContact() {
this.nautic.createContact({
body: {
name: 'Ron Swanson'
}
})
}

findRonSwansonContact() {
this.nautic.findContact({ param: 'name', value: 'Ron Swanson' })
}

updateRonSwansonContact() {
this.nautic.updateContact({ id: '1', company: 'Pawnee City Council' })
}
}
61 changes: 61 additions & 0 deletions tests/test.js
@@ -0,0 +1,61 @@
import Nautic from '../index'
import NauticConsumer from './nautic-consumer'

jest.mock('../index')

beforeEach(() => {
Nautic.mockClear()
})

it('Check if the consumer called the createContact method on the class instance', () => {
expect(Nautic).not.toHaveBeenCalled()

const nauticConsumer = new NauticConsumer()
expect(Nautic).toHaveBeenCalledTimes(1)

nauticConsumer.createNewContact()

const [mockNauticInstance] = Nautic.mock.instances
const mockCreateContact = mockNauticInstance.createContact

expect(mockCreateContact).toHaveBeenCalledWith({
body: { name: 'Ron Swanson' }
})
expect(mockCreateContact).toHaveBeenCalledTimes(1)
})

it('Check if the consumer called the findContact method on the class instance', () => {
expect(Nautic).not.toHaveBeenCalled()

const nauticConsumer = new NauticConsumer()
expect(Nautic).toHaveBeenCalledTimes(1)

nauticConsumer.findRonSwansonContact()

const [mockNauticInstance] = Nautic.mock.instances
const mockFindContact = mockNauticInstance.findContact

expect(mockFindContact).toHaveBeenCalledWith({
param: 'name',
value: 'Ron Swanson'
})
expect(mockFindContact).toHaveBeenCalledTimes(1)
})

it('Check if the consumer called the updateContact method on the class instance', () => {
expect(Nautic).not.toHaveBeenCalled()

const nauticConsumer = new NauticConsumer()
expect(Nautic).toHaveBeenCalledTimes(1)

nauticConsumer.updateRonSwansonContact()

const [mockNauticInstance] = Nautic.mock.instances
const mockUpdateContact = mockNauticInstance.updateContact

expect(mockUpdateContact).toHaveBeenCalledWith({
id: '1',
company: 'Pawnee City Council'
})
expect(mockUpdateContact).toHaveBeenCalledTimes(1)
})

0 comments on commit c6b807d

Please sign in to comment.