Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import validators from '../../../validators'
- const {StoreRecipeValidator} = validators
- describe('Store recipe validator class', ()=> {
- describe('validateTitle()', ()=> {
- test('adds an error to the array if the recipe title is <5 chars', ()=>{
- const validator = new StoreRecipeValidator ({
- title: 'M'
- })
- validator.validateTitle()
- expect(validator.errors).toEqual(['The title must be longer than 5 characters.'])
- })
- test('adds an error to the array if title is not provided', ()=>{
- const validator = new StoreRecipeValidator ({
- description: 'food is good'
- })
- validator.validateTitle()
- expect(validator.errors).toEqual(['The title is required.'])
- })
- })
- describe('validateDescription()', ()=> {
- test('adds an error to the array if the recipe description is <5 chars', ()=>{
- const validator = new StoreRecipeValidator ({
- description: 'M'
- })
- validator.validateDescription()
- expect(validator.errors).toEqual(['The description must be longer than 5 characters.'])
- })
- test('adds an error to the array if description is not provided', ()=>{
- const validator = new StoreRecipeValidator ({
- title: 'bonza food particles!'
- })
- validator.validateDescription()
- expect(validator.errors).toEqual(['The description is required.'])
- })
- })
- describe('validateTimeToCook()', ()=> {
- test('an error is added to the array is a number is not passed', ()=>{
- const validator = new StoreRecipeValidator ({
- timeToCook: 'ten minutes'
- })
- validator.validateTimeToCook()
- expect(validator.errors).toEqual(['The time to cook must be a number in minutes.'])
- })
- test('adds an error to the array if time is not provided', ()=>{
- const validator = new StoreRecipeValidator ({
- title: 'bonza food particles!'
- })
- validator.validateTimeToCook()
- expect(validator.errors).toEqual(['The time to cook is required.'])
- })
- })
- describe('validateIngredients()', ()=> {
- test('adds an error to the array if ingredients are not provided', ()=>{
- const validator = new StoreRecipeValidator ({
- title: 'bonza food particles!'
- })
- validator.validateIngredients()
- expect(validator.errors).toEqual(['The ingredients are required.'])
- })
- test('adds an error to the array if ingredients are not a json list', ()=>{
- const validator = new StoreRecipeValidator ({
- "ingredients": "5 cups moonshine, 1 tea bag, a brick"
- })
- validator.validateIngredients()
- expect(validator.errors).toEqual(['The ingredients must be a json list of ingredients.'])
- })
- test('adds an error to the array if there is an empty array of ingredients', ()=>{
- const validator = new StoreRecipeValidator ({
- "ingredients": "[]"
- })
- validator.validateIngredients()
- expect(validator.errors).toEqual(['There must be at least one ingredient.'])
- })
- test('adds an error to the array if the list in not an array', ()=>{
- const validator = new StoreRecipeValidator ({
- "ingredients": "{\"only ingredient\":\"two fingers\"}"
- })
- validator.validateIngredients()
- expect(validator.errors).toEqual(['There must be a list of ingredients.'])
- })
- })
- describe('validateImageUrl()', ()=> {
- test('adds an error to the array if there is no image', ()=>{
- const validator = new StoreRecipeValidator ({
- imageUrl: ""
- })
- validator.validateImageUrl()
- expect(validator.errors).toEqual(['The image url is required.'])
- })
- test('adds an error to the array if the imageUrl is not valid', ()=>{
- const validator = new StoreRecipeValidator ({
- imageUrl: "i.imgur.com/av7fjeAjpg"
- })
- validator.validateImageUrl()
- expect(validator.errors).toEqual(['The image url must be a valid web url.'])
- })
- })
- describe('validateProcedure()', ()=> {
- test('adds an error to the array if procedure are not provided', ()=>{
- const validator = new StoreRecipeValidator ({
- title: 'bonza food particles!'
- })
- validator.validateProcedure()
- expect(validator.errors).toEqual(['The procedure is required.'])
- })
- test('adds an error to the array if procedure are not a json list', ()=>{
- const validator = new StoreRecipeValidator ({
- "procedure": "blend moonshine, suck tea bag, eat brick"
- })
- validator.validateProcedure()
- expect(validator.errors).toEqual(['The procedure must be a json of procedural steps.'])
- })
- test('adds an error to the array if there is an empty array of procedure', ()=>{
- const validator = new StoreRecipeValidator ({
- "procedure": "[]"
- })
- validator.validateProcedure()
- expect(validator.errors).toEqual(['There must be at least one procedure step.'])
- })
- test('adds an error to the array if the list in not an array', ()=>{
- const validator = new StoreRecipeValidator ({
- "procedure": "{\"only ingredient\":\"two fingers\"}"
- })
- validator.validateProcedure()
- expect(validator.errors).toEqual(['There must be a list of procedure steps.'])
- })
- test('isValid() calls validateTitle(), validateDescription(), validateTimeToCook(), validateImageUrl(), validateIngredients(), and, validateProcedure()', async ()=> {
- const validator = new StoreRecipeValidator({
- title : "Butter Pie",
- description : "A local Northern English Indelicacy",
- timeToCook : 45,
- imageUrl : "https://www.greedygourmet.com/wordpress/wp-content/uploads/2017/11/lancashirebutterpie.jpg",
- ingredients : "[\"Pie\", \"Butter\", \"Potato\"]",
- procedure: "[\"blend pie into butter\", \"add potato\", \"bake 7000f for 12 days\"]"
- })
- validator.validateTitle();
- validator.validateDescription();
- validator.validateTimeToCook();
- validator.validateImageUrl();
- validator.validateIngredients();
- validator.validateProcedure();
- jest.spyOn(validator, "validateTitle")
- jest.spyOn(validator, "validateDescription")
- jest.spyOn(validator, "validateTimeToCook")
- jest.spyOn(validator, "validateImageUrl")
- jest.spyOn(validator, "validateIngredients")
- jest.spyOn(validator, "validateProcedure")
- const result = await validator.isValid()
- expect(validator.validateTitle).toHaveBeenCalled()
- expect(validator.validateDescription).toHaveBeenCalled()
- expect(validator.validateTimeToCook).toHaveBeenCalled()
- expect(validator.validateImageUrl).toHaveBeenCalled()
- expect(validator.validateIngredients).toHaveBeenCalled()
- expect(validator.validateProcedure).toHaveBeenCalled()
- })
- })
- test('isValid() returns false with invalid recipe', async ()=> {
- const validator = new StoreRecipeValidator({
- title : "Butter Pie",
- description : "A local Northern English Indelicacy",
- timeToCook : "forty five",
- imageUrl : "https://www.greedygourmet.com/wordpress/wp-content/uploads/2017/11/lancashirebutterpie.jpg",
- ingredients : "[\"Pie\", \"Butter\", \"Potato\"]",
- procedure: "[\"blend pie into butter\", \"add potato\", \"bake 7000f for 12 days\"]"
- })
- const result = await validator.isValid()
- expect(result).toEqual(false)
- })
- test('isValid() adds an error with no recipe', async ()=> {
- const validator = new StoreRecipeValidator()
- validator.isValid()
- expect(validator.errors).toEqual(['No recipe was provided.'])
- })
- test('isValid() returns true with valid recipe', async ()=> {
- const validator = new StoreRecipeValidator({
- title : "Butter Pie",
- description : "A local Northern English Indelicacy",
- timeToCook : 123,
- imageUrl : "https://www.greedygourmet.com/wordpress/wp-content/uploads/2017/11/lancashirebutterpie.jpg",
- ingredients : "[\"Pie\", \"Butter\", \"Potato\"]",
- procedure: "[\"blend pie into butter\", \"add potato\", \"bake 7000f for 12 days\"]"
- })
- const result = await validator.isValid()
- expect(result).toEqual(true)
- })
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement