Advertisement
andrewAtkinson

store recipe tests

Feb 22nd, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import validators from '../../../validators'
  2. const {StoreRecipeValidator} = validators
  3.  
  4. describe('Store recipe validator class', ()=> {
  5.  
  6.   describe('validateTitle()', ()=> {
  7.     test('adds an error to the array if the recipe title is <5 chars', ()=>{
  8.       const validator = new StoreRecipeValidator ({
  9.         title: 'M'
  10.       })
  11.       validator.validateTitle()
  12.       expect(validator.errors).toEqual(['The title must be longer than 5 characters.'])
  13.     })
  14.    
  15.     test('adds an error to the array if title is not provided', ()=>{
  16.       const validator = new StoreRecipeValidator ({
  17.         description: 'food is good'
  18.       })
  19.       validator.validateTitle()
  20.       expect(validator.errors).toEqual(['The title is required.'])
  21.     })
  22.   })
  23.  
  24.   describe('validateDescription()', ()=> {
  25.     test('adds an error to the array if the recipe description is <5 chars', ()=>{
  26.       const validator = new StoreRecipeValidator ({
  27.         description: 'M'
  28.       })
  29.       validator.validateDescription()
  30.       expect(validator.errors).toEqual(['The description must be longer than 5 characters.'])
  31.     })
  32.  
  33.     test('adds an error to the array if description is not provided', ()=>{
  34.       const validator = new StoreRecipeValidator ({
  35.         title: 'bonza food particles!'
  36.       })
  37.       validator.validateDescription()
  38.       expect(validator.errors).toEqual(['The description is required.'])
  39.     })
  40.   })
  41.  
  42.   describe('validateTimeToCook()', ()=> {
  43.     test('an error is added to the array is a number is not passed', ()=>{
  44.       const validator = new StoreRecipeValidator ({
  45.         timeToCook: 'ten minutes'
  46.       })
  47.       validator.validateTimeToCook()
  48.       expect(validator.errors).toEqual(['The time to cook must be a number in minutes.'])
  49.     })
  50.  
  51.     test('adds an error to the array if time is not provided', ()=>{
  52.       const validator = new StoreRecipeValidator ({
  53.         title: 'bonza food particles!'
  54.       })
  55.       validator.validateTimeToCook()
  56.       expect(validator.errors).toEqual(['The time to cook is required.'])
  57.     })
  58.   })
  59.  
  60.   describe('validateIngredients()', ()=> {
  61.     test('adds an error to the array if ingredients are not provided', ()=>{
  62.       const validator = new StoreRecipeValidator ({
  63.         title: 'bonza food particles!'
  64.       })
  65.       validator.validateIngredients()
  66.       expect(validator.errors).toEqual(['The ingredients are required.'])
  67.     })
  68.  
  69.     test('adds an error to the array if ingredients are not a json list', ()=>{
  70.       const validator = new StoreRecipeValidator ({
  71.         "ingredients": "5 cups moonshine, 1 tea bag, a brick"
  72.         })
  73.       validator.validateIngredients()
  74.       expect(validator.errors).toEqual(['The ingredients must be a json list of ingredients.'])
  75.     })
  76.     test('adds an error to the array if there is an empty array of ingredients', ()=>{
  77.       const validator = new StoreRecipeValidator ({
  78.         "ingredients": "[]"
  79.         })
  80.       validator.validateIngredients()
  81.       expect(validator.errors).toEqual(['There must be at least one ingredient.'])
  82.     })
  83.  
  84.     test('adds an error to the array if the list in not an array', ()=>{
  85.       const validator = new StoreRecipeValidator ({
  86.         "ingredients": "{\"only ingredient\":\"two fingers\"}"
  87.       })
  88.       validator.validateIngredients()
  89.       expect(validator.errors).toEqual(['There must be a list of ingredients.'])
  90.     })
  91.   })
  92.  
  93.   describe('validateImageUrl()', ()=> {
  94.     test('adds an error to the array if there is no image', ()=>{
  95.       const validator = new StoreRecipeValidator ({
  96.         imageUrl: ""
  97.       })
  98.       validator.validateImageUrl()
  99.       expect(validator.errors).toEqual(['The image url is required.'])
  100.     })
  101.  
  102.     test('adds an error to the array if the imageUrl is not valid', ()=>{
  103.       const validator = new StoreRecipeValidator ({
  104.         imageUrl: "i.imgur.com/av7fjeAjpg"
  105.       })
  106.       validator.validateImageUrl()
  107.       expect(validator.errors).toEqual(['The image url must be a valid web url.'])
  108.     })
  109.   })
  110.  
  111.  
  112.   describe('validateProcedure()', ()=> {
  113.     test('adds an error to the array if procedure are not provided', ()=>{
  114.       const validator = new StoreRecipeValidator ({
  115.         title: 'bonza food particles!'
  116.       })
  117.       validator.validateProcedure()
  118.       expect(validator.errors).toEqual(['The procedure is required.'])
  119.     })
  120.  
  121.     test('adds an error to the array if procedure are not a json list', ()=>{
  122.       const validator = new StoreRecipeValidator ({
  123.         "procedure": "blend moonshine, suck tea bag, eat brick"
  124.         })
  125.       validator.validateProcedure()
  126.       expect(validator.errors).toEqual(['The procedure must be a json of procedural steps.'])
  127.     })
  128.  
  129.     test('adds an error to the array if there is an empty array of procedure', ()=>{
  130.       const validator = new StoreRecipeValidator ({
  131.         "procedure": "[]"
  132.         })
  133.       validator.validateProcedure()
  134.       expect(validator.errors).toEqual(['There must be at least one procedure step.'])
  135.     })
  136.  
  137.     test('adds an error to the array if the list in not an array', ()=>{
  138.       const validator = new StoreRecipeValidator ({
  139.         "procedure": "{\"only ingredient\":\"two fingers\"}"
  140.       })
  141.       validator.validateProcedure()
  142.       expect(validator.errors).toEqual(['There must be a list of procedure steps.'])
  143.     })
  144.  
  145.     test('isValid() calls validateTitle(), validateDescription(), validateTimeToCook(), validateImageUrl(), validateIngredients(), and, validateProcedure()', async ()=> {
  146.       const validator = new StoreRecipeValidator({
  147.         title : "Butter Pie",
  148.         description : "A local Northern English Indelicacy",
  149.         timeToCook : 45,
  150.         imageUrl : "https://www.greedygourmet.com/wordpress/wp-content/uploads/2017/11/lancashirebutterpie.jpg",
  151.         ingredients : "[\"Pie\", \"Butter\", \"Potato\"]",
  152.         procedure: "[\"blend pie into butter\", \"add potato\", \"bake 7000f for 12 days\"]"
  153.       })
  154.  
  155.       validator.validateTitle();
  156.       validator.validateDescription();
  157.       validator.validateTimeToCook();
  158.       validator.validateImageUrl();
  159.       validator.validateIngredients();
  160.       validator.validateProcedure();
  161.  
  162.       jest.spyOn(validator, "validateTitle")
  163.       jest.spyOn(validator, "validateDescription")
  164.       jest.spyOn(validator, "validateTimeToCook")
  165.       jest.spyOn(validator, "validateImageUrl")
  166.       jest.spyOn(validator, "validateIngredients")
  167.       jest.spyOn(validator, "validateProcedure")
  168.  
  169.       const result = await validator.isValid()
  170.  
  171.       expect(validator.validateTitle).toHaveBeenCalled()
  172.       expect(validator.validateDescription).toHaveBeenCalled()
  173.       expect(validator.validateTimeToCook).toHaveBeenCalled()
  174.       expect(validator.validateImageUrl).toHaveBeenCalled()
  175.       expect(validator.validateIngredients).toHaveBeenCalled()
  176.       expect(validator.validateProcedure).toHaveBeenCalled()
  177.     })
  178.   })
  179.  
  180.   test('isValid() returns false with invalid recipe', async ()=> {
  181.     const validator = new StoreRecipeValidator({
  182.       title : "Butter Pie",
  183.       description : "A local Northern English Indelicacy",
  184.       timeToCook : "forty five",
  185.       imageUrl : "https://www.greedygourmet.com/wordpress/wp-content/uploads/2017/11/lancashirebutterpie.jpg",
  186.       ingredients : "[\"Pie\", \"Butter\", \"Potato\"]",
  187.       procedure: "[\"blend pie into butter\", \"add potato\", \"bake 7000f for 12 days\"]"
  188.     })
  189.     const result = await validator.isValid()
  190.     expect(result).toEqual(false)
  191.   })
  192.  
  193.   test('isValid() adds an error with no recipe', async ()=> {
  194.     const validator = new StoreRecipeValidator()
  195.     validator.isValid()
  196.     expect(validator.errors).toEqual(['No recipe was provided.'])
  197.   })
  198.  
  199.   test('isValid() returns true with valid recipe', async ()=> {
  200.     const validator = new StoreRecipeValidator({
  201.       title : "Butter Pie",
  202.       description : "A local Northern English Indelicacy",
  203.       timeToCook : 123,
  204.       imageUrl : "https://www.greedygourmet.com/wordpress/wp-content/uploads/2017/11/lancashirebutterpie.jpg",
  205.       ingredients : "[\"Pie\", \"Butter\", \"Potato\"]",
  206.       procedure: "[\"blend pie into butter\", \"add potato\", \"bake 7000f for 12 days\"]"
  207.     })
  208.     const result = await validator.isValid()
  209.     expect(result).toEqual(true)
  210.   })
  211. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement