-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test coverage for ExportUsers class
- Loading branch information
Showing
7 changed files
with
120 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' } | ||
] | ||
} | ||
) | ||
}) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
) | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}]) | ||
}) | ||
}) | ||
}) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters