Frisby.js testing with optional array of objects












0















How can you use Frisby.js to test an optional array that contains objects? For example, say we have an API call that returns something like this:



{
"id": "123",
"type": "A",
"list": [
{
"id": "111",
"size": 1
},
{
"id": "222",
"size": 2
}
]
}


However, it may also return something like this:



{
"id": "456",
"type": "B"
}


Currently, I'm trying:



const frisby = require('frisby');
const Joi = frisby.Joi;

test('myTest', () => {
return frisby
.get(myUrl)
.expect('status', 200)
.expect('jsonTypes', {
id: Joi.string().required(),
type: Joi.string().required().
list: Joi.array().optional()
})
.expect('jsonTypes', 'list.*', {
id: Joi.string().required(),
size: Joi.number().required()
});
});


This won't work, however, since the path (list.*) won't be defined if the list attribute doesn't exist. Any ideas?










share|improve this question





























    0















    How can you use Frisby.js to test an optional array that contains objects? For example, say we have an API call that returns something like this:



    {
    "id": "123",
    "type": "A",
    "list": [
    {
    "id": "111",
    "size": 1
    },
    {
    "id": "222",
    "size": 2
    }
    ]
    }


    However, it may also return something like this:



    {
    "id": "456",
    "type": "B"
    }


    Currently, I'm trying:



    const frisby = require('frisby');
    const Joi = frisby.Joi;

    test('myTest', () => {
    return frisby
    .get(myUrl)
    .expect('status', 200)
    .expect('jsonTypes', {
    id: Joi.string().required(),
    type: Joi.string().required().
    list: Joi.array().optional()
    })
    .expect('jsonTypes', 'list.*', {
    id: Joi.string().required(),
    size: Joi.number().required()
    });
    });


    This won't work, however, since the path (list.*) won't be defined if the list attribute doesn't exist. Any ideas?










    share|improve this question



























      0












      0








      0








      How can you use Frisby.js to test an optional array that contains objects? For example, say we have an API call that returns something like this:



      {
      "id": "123",
      "type": "A",
      "list": [
      {
      "id": "111",
      "size": 1
      },
      {
      "id": "222",
      "size": 2
      }
      ]
      }


      However, it may also return something like this:



      {
      "id": "456",
      "type": "B"
      }


      Currently, I'm trying:



      const frisby = require('frisby');
      const Joi = frisby.Joi;

      test('myTest', () => {
      return frisby
      .get(myUrl)
      .expect('status', 200)
      .expect('jsonTypes', {
      id: Joi.string().required(),
      type: Joi.string().required().
      list: Joi.array().optional()
      })
      .expect('jsonTypes', 'list.*', {
      id: Joi.string().required(),
      size: Joi.number().required()
      });
      });


      This won't work, however, since the path (list.*) won't be defined if the list attribute doesn't exist. Any ideas?










      share|improve this question
















      How can you use Frisby.js to test an optional array that contains objects? For example, say we have an API call that returns something like this:



      {
      "id": "123",
      "type": "A",
      "list": [
      {
      "id": "111",
      "size": 1
      },
      {
      "id": "222",
      "size": 2
      }
      ]
      }


      However, it may also return something like this:



      {
      "id": "456",
      "type": "B"
      }


      Currently, I'm trying:



      const frisby = require('frisby');
      const Joi = frisby.Joi;

      test('myTest', () => {
      return frisby
      .get(myUrl)
      .expect('status', 200)
      .expect('jsonTypes', {
      id: Joi.string().required(),
      type: Joi.string().required().
      list: Joi.array().optional()
      })
      .expect('jsonTypes', 'list.*', {
      id: Joi.string().required(),
      size: Joi.number().required()
      });
      });


      This won't work, however, since the path (list.*) won't be defined if the list attribute doesn't exist. Any ideas?







      javascript testing web-api-testing joi frisby.js






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 22:27







      Connor

















      asked Nov 19 '18 at 23:22









      ConnorConnor

      11




      11
























          2 Answers
          2






          active

          oldest

          votes


















          0














          I have a work around until someone figures out a more efficient method. This method just seems inefficient because it requires multiple API calls. The gist is to first check if the optional array exists. If so, you can use another API call and use the path to make the appropriate checks.



          const frisby = require('frisby');
          const Joi = frisby.Joi;

          test('myTest', () => {
          return frisby
          .get(myUrl)
          .expect('status', 200)
          .expect('jsonTypes', {
          id: Joi.string().required(),
          type: Joi.string().required().
          list: Joi.array().optional()
          })
          .then(function (response) {
          if(response._json.list) {
          return frisby
          .get(myUrl)
          .expect('jsonTypes', 'list.*', {
          id: Joi.string().required(),
          size: Joi.number().required()
          });
          }
          });
          });





          share|improve this answer































            0














            By this way you can do that :



                 .expect('jsonTypes',"data", {
            id: Joi.number(),
            type: Joi.string()
            })
            .expect('jsonTypes', "data.list",Joi.array().items({
            "id": Joi.number(),
            "size": Joi.number()
            }))
            .then(function(res) {
            var body = res.body;
            body = JSON.parse(body);

            expect(body.data.id).toBeDefined();
            expect(body.data.type).toBeDefined();
            if(body.data.list) {
            for(var i = 0; i < body.data.list.length; i++){
            expect(body.data.list[i].id).toBeDefined();
            expect(body.data.list[i].type).toBeDefined();
            }
            }





            share|improve this answer

























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384074%2ffrisby-js-testing-with-optional-array-of-objects%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              I have a work around until someone figures out a more efficient method. This method just seems inefficient because it requires multiple API calls. The gist is to first check if the optional array exists. If so, you can use another API call and use the path to make the appropriate checks.



              const frisby = require('frisby');
              const Joi = frisby.Joi;

              test('myTest', () => {
              return frisby
              .get(myUrl)
              .expect('status', 200)
              .expect('jsonTypes', {
              id: Joi.string().required(),
              type: Joi.string().required().
              list: Joi.array().optional()
              })
              .then(function (response) {
              if(response._json.list) {
              return frisby
              .get(myUrl)
              .expect('jsonTypes', 'list.*', {
              id: Joi.string().required(),
              size: Joi.number().required()
              });
              }
              });
              });





              share|improve this answer




























                0














                I have a work around until someone figures out a more efficient method. This method just seems inefficient because it requires multiple API calls. The gist is to first check if the optional array exists. If so, you can use another API call and use the path to make the appropriate checks.



                const frisby = require('frisby');
                const Joi = frisby.Joi;

                test('myTest', () => {
                return frisby
                .get(myUrl)
                .expect('status', 200)
                .expect('jsonTypes', {
                id: Joi.string().required(),
                type: Joi.string().required().
                list: Joi.array().optional()
                })
                .then(function (response) {
                if(response._json.list) {
                return frisby
                .get(myUrl)
                .expect('jsonTypes', 'list.*', {
                id: Joi.string().required(),
                size: Joi.number().required()
                });
                }
                });
                });





                share|improve this answer


























                  0












                  0








                  0







                  I have a work around until someone figures out a more efficient method. This method just seems inefficient because it requires multiple API calls. The gist is to first check if the optional array exists. If so, you can use another API call and use the path to make the appropriate checks.



                  const frisby = require('frisby');
                  const Joi = frisby.Joi;

                  test('myTest', () => {
                  return frisby
                  .get(myUrl)
                  .expect('status', 200)
                  .expect('jsonTypes', {
                  id: Joi.string().required(),
                  type: Joi.string().required().
                  list: Joi.array().optional()
                  })
                  .then(function (response) {
                  if(response._json.list) {
                  return frisby
                  .get(myUrl)
                  .expect('jsonTypes', 'list.*', {
                  id: Joi.string().required(),
                  size: Joi.number().required()
                  });
                  }
                  });
                  });





                  share|improve this answer













                  I have a work around until someone figures out a more efficient method. This method just seems inefficient because it requires multiple API calls. The gist is to first check if the optional array exists. If so, you can use another API call and use the path to make the appropriate checks.



                  const frisby = require('frisby');
                  const Joi = frisby.Joi;

                  test('myTest', () => {
                  return frisby
                  .get(myUrl)
                  .expect('status', 200)
                  .expect('jsonTypes', {
                  id: Joi.string().required(),
                  type: Joi.string().required().
                  list: Joi.array().optional()
                  })
                  .then(function (response) {
                  if(response._json.list) {
                  return frisby
                  .get(myUrl)
                  .expect('jsonTypes', 'list.*', {
                  id: Joi.string().required(),
                  size: Joi.number().required()
                  });
                  }
                  });
                  });






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 26 '18 at 22:26









                  ConnorConnor

                  11




                  11

























                      0














                      By this way you can do that :



                           .expect('jsonTypes',"data", {
                      id: Joi.number(),
                      type: Joi.string()
                      })
                      .expect('jsonTypes', "data.list",Joi.array().items({
                      "id": Joi.number(),
                      "size": Joi.number()
                      }))
                      .then(function(res) {
                      var body = res.body;
                      body = JSON.parse(body);

                      expect(body.data.id).toBeDefined();
                      expect(body.data.type).toBeDefined();
                      if(body.data.list) {
                      for(var i = 0; i < body.data.list.length; i++){
                      expect(body.data.list[i].id).toBeDefined();
                      expect(body.data.list[i].type).toBeDefined();
                      }
                      }





                      share|improve this answer






























                        0














                        By this way you can do that :



                             .expect('jsonTypes',"data", {
                        id: Joi.number(),
                        type: Joi.string()
                        })
                        .expect('jsonTypes', "data.list",Joi.array().items({
                        "id": Joi.number(),
                        "size": Joi.number()
                        }))
                        .then(function(res) {
                        var body = res.body;
                        body = JSON.parse(body);

                        expect(body.data.id).toBeDefined();
                        expect(body.data.type).toBeDefined();
                        if(body.data.list) {
                        for(var i = 0; i < body.data.list.length; i++){
                        expect(body.data.list[i].id).toBeDefined();
                        expect(body.data.list[i].type).toBeDefined();
                        }
                        }





                        share|improve this answer




























                          0












                          0








                          0







                          By this way you can do that :



                               .expect('jsonTypes',"data", {
                          id: Joi.number(),
                          type: Joi.string()
                          })
                          .expect('jsonTypes', "data.list",Joi.array().items({
                          "id": Joi.number(),
                          "size": Joi.number()
                          }))
                          .then(function(res) {
                          var body = res.body;
                          body = JSON.parse(body);

                          expect(body.data.id).toBeDefined();
                          expect(body.data.type).toBeDefined();
                          if(body.data.list) {
                          for(var i = 0; i < body.data.list.length; i++){
                          expect(body.data.list[i].id).toBeDefined();
                          expect(body.data.list[i].type).toBeDefined();
                          }
                          }





                          share|improve this answer















                          By this way you can do that :



                               .expect('jsonTypes',"data", {
                          id: Joi.number(),
                          type: Joi.string()
                          })
                          .expect('jsonTypes', "data.list",Joi.array().items({
                          "id": Joi.number(),
                          "size": Joi.number()
                          }))
                          .then(function(res) {
                          var body = res.body;
                          body = JSON.parse(body);

                          expect(body.data.id).toBeDefined();
                          expect(body.data.type).toBeDefined();
                          if(body.data.list) {
                          for(var i = 0; i < body.data.list.length; i++){
                          expect(body.data.list[i].id).toBeDefined();
                          expect(body.data.list[i].type).toBeDefined();
                          }
                          }






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Feb 9 at 5:26

























                          answered Feb 9 at 5:11









                          DevArtiDevArti

                          1




                          1






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384074%2ffrisby-js-testing-with-optional-array-of-objects%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Guess what letter conforming each word

                              Port of Spain

                              Run scheduled task as local user group (not BUILTIN)