Skip to content

Commit

Permalink
Add test coverage for ExportUsers class
Browse files Browse the repository at this point in the history
  • Loading branch information
mjgiarlo committed May 2, 2019
1 parent 6ce1065 commit 945e45a
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 4 deletions.
7 changes: 6 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@
}]],
"presets": [
"@babel/preset-env"
]
],
"env": {
"test": {
"plugins": ["@babel/plugin-transform-runtime"]
}
}
}
18 changes: 18 additions & 0 deletions __mocks__/cognito-fake.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export default class CognitoFake {
listUsers() {
return {
promise: async () => {
return new Promise(resolve => {
return resolve(
{
Users: [
{ Username: 'user1' },
{ Username: 'user2' }
]
}
)
})
}
}
}
}
33 changes: 33 additions & 0 deletions __mocks__/cognito-pagination-fake.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export default class CognitoPaginationFake {
constructor() {
this.calledAlready = false
}

listUsers() {
return {
promise: async () => {
return new Promise(resolve => {
if (this.calledAlready) {
return resolve(
{
Users: [
{ Username: 'user2' }
]
}
)
} else {
this.calledAlready= true
return resolve(
{
Users: [
{ Username: 'user1' }
],
PaginationToken: 'token'
}
)
}
})
}
}
}
}
50 changes: 48 additions & 2 deletions __tests__/ExportUsers.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,51 @@
import AWS from 'aws-sdk'
import ExportUsers from '../src/ExportUsers'
import CognitoFake from '../__mocks__/cognito-fake'
import CognitoPaginationFake from '../__mocks__/cognito-pagination-fake'

describe('ExportUsers', () => {
test('dummy test', () => {
expect(true).toEqual(true)
const exporter = new ExportUsers()

describe('constructor()', () => {
it('sets cognito', () => {
expect(exporter.cognito).toBeInstanceOf(AWS.CognitoIdentityServiceProvider)
})
})

describe('export()', () => {
const logSpy = jest.spyOn(console, 'log')

describe('without pagination', () => {
beforeAll(() => {
exporter.cognito = new CognitoFake()
})

it('logs a message', async () => {
await exporter.export()
expect(logSpy).toHaveBeenCalledWith('Users exported from Cognito')
})

it('returns a list of users', async () => {
const userList = await exporter.export()
expect(userList).toEqual([{"Username":"user1"},{"Username":"user2"}])
})
})

describe('with pagination', () => {
// NOTE: Using beforeEach instead of beforeAll because the pagination fake stores the state of whether listUsers() has been called already
beforeEach(() => {
exporter.cognito = new CognitoPaginationFake()
})

it('logs a message', async () => {
await exporter.export()
expect(logSpy).toHaveBeenCalledWith('Users exported from Cognito')
})

it('returns a list of users', async () => {
const userList = await exporter.export()
expect(userList).toEqual([{"Username":"user1"},{"Username":"user2"}])
})
})
})
})
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
}
],
"devDependencies": {
"@babel/plugin-transform-runtime": "^7.4.4",
"babel-eslint": "^10.0.1",
"eslint": "^5.16.0",
"eslint-plugin-import": "^2.17.2",
Expand Down
3 changes: 2 additions & 1 deletion src/ExportUsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ export default class ExportUsers {

const paginationCalls = async (exportedUsers = []) => {
const { Users = [], PaginationToken } = await this.cognito.listUsers(params).promise()

const combinedUsers = exportedUsers.concat(Users)

if (PaginationToken) {
params.PaginationToken = PaginationToken
await paginationCalls(combinedUsers)
return await paginationCalls(combinedUsers)
}

return combinedUsers
Expand Down

0 comments on commit 945e45a

Please sign in to comment.