Created
October 25, 2019 17:14
-
-
Save maldonadod/3d83517865edbba30ab093a783c6d54a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const newUserRegistrationUseCase = require("../Domain/newUserRegistrationUseCase") | |
const newNameFactory = require("../Domain/newFactoryName") | |
const newAccountRepository = require("../Domain/newAccountRepository") | |
const newRegisterPresenter = require("../Domain/newRegisterPresenter") | |
describe("UserRegistrationShould", UserRegistrationShould) | |
function UserRegistrationShould() { | |
it("should not register invalid names: due emptyness", ShouldDisplayFailedRegisterAttemptDueEmptyness) | |
it("should not register invalid names: due minimum length", ShouldDisplayFailedRegisterAttemptDueMinimumLength) | |
it("should not register invalid names: due duplication", ShouldDisplayFailedRegisterAttemptDueDuplication) | |
it("should register valid name", ShouldDisplaySuccessfulRegister) | |
} | |
function exercise(name, verify, registedNames) { | |
const aRegisterPresenter = newRegisterPresenter(); | |
aRegisterPresenter.showFailedRegistration = jest.fn() | |
aRegisterPresenter.showSuccessfulRegistration = jest.fn() | |
const aNameFactory = newNameFactory(); | |
const anAccountRepository = newAccountRepository(registedNames); | |
anAccountRepository.save = jest.fn(); | |
newUserRegistrationUseCase( | |
aRegisterPresenter, | |
aNameFactory, | |
anAccountRepository | |
).execute(name) | |
verify(aRegisterPresenter, anAccountRepository) | |
} | |
function ShouldDisplayFailedRegisterAttemptDueEmptyness() { | |
const name = "" | |
exercise(name, (aRegisterPresenter, anAccountRepository) => { | |
expect(anAccountRepository.save).not.toBeCalled() | |
expect(aRegisterPresenter.showFailedRegistration).toHaveBeenCalledWith("name can not be empty") | |
}) | |
} | |
function ShouldDisplayFailedRegisterAttemptDueMinimumLength() { | |
const name = "abc" | |
exercise(name, (aRegisterPresenter, anAccountRepository) => { | |
expect(anAccountRepository.save).not.toBeCalled() | |
expect(aRegisterPresenter.showFailedRegistration).toHaveBeenCalledWith("at least 4 letters!") | |
}) | |
} | |
function ShouldDisplayFailedRegisterAttemptDueDuplication() { | |
const name = "dani" | |
const registedNames = [name]; | |
exercise(name, (aRegisterPresenter, anAccountRepository) => { | |
expect(anAccountRepository.save).not.toBeCalled() | |
expect(aRegisterPresenter.showFailedRegistration).toHaveBeenCalledWith("that name is taken") | |
}, registedNames) | |
} | |
function ShouldDisplaySuccessfulRegister() { | |
const name = "dani" | |
exercise(name, (aRegisterPresenter, anAccountRepository) => { | |
expect(anAccountRepository.save).toHaveBeenCalledWith("dani") | |
expect(aRegisterPresenter.showSuccessfulRegistration).toBeCalled() | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment