How to use newJson method in argos

Best JavaScript code snippet using argos

model_tags_spec.js

Source:model_tags_spec.js Github

copy

Full Screen

1/*globals describe, before, beforeEach, afterEach, it */2/*jshint expr:true*/3var testUtils = require('../../utils'),4 should = require('should'),5 sinon = require('sinon'),6 Promise = require('bluebird'),7 _ = require('lodash'),8 // Stuff we are testing9 ModelsTag = require('../../../server/models/tag'),10 ModelsPost = require('../../../server/models/post'),11 events = require('../../../server/events'),12 context = testUtils.context.admin,13 TagModel,14 PostModel,15 sandbox = sinon.sandbox.create();16describe('Tag Model', function () {17 var eventSpy;18 // Keep the DB clean19 before(testUtils.teardown);20 afterEach(testUtils.teardown);21 beforeEach(testUtils.setup());22 afterEach(function () {23 sandbox.restore();24 });25 beforeEach(function () {26 eventSpy = sandbox.spy(events, 'emit');27 });28 before(function () {29 TagModel = ModelsTag.Tag;30 PostModel = ModelsPost.Post;31 should.exist(TagModel);32 should.exist(PostModel);33 });34 it('uses Date objects for dateTime fields', function (done) {35 TagModel.add(testUtils.DataGenerator.forModel.tags[0], context).then(function (tag) {36 return TagModel.findOne({id: tag.id});37 }).then(function (tag) {38 should.exist(tag);39 tag.get('created_at').should.be.an.instanceof(Date);40 done();41 }).catch(done);42 });43 it('returns post_count if include post_count', function (done) {44 testUtils.fixtures.insertPosts().then(function () {45 TagModel.findOne({slug: 'kitchen-sink'}, {include: 'post_count'}).then(function (tag) {46 should.exist(tag);47 tag.get('post_count').should.equal(2);48 done();49 }).catch(done);50 });51 });52 describe('findPage', function () {53 beforeEach(function (done) {54 testUtils.fixtures.insertPosts().then(function () {55 done();56 }).catch(done);57 });58 it('with limit all', function (done) {59 TagModel.findPage({limit: 'all'}).then(function (results) {60 results.meta.pagination.page.should.equal(1);61 results.meta.pagination.limit.should.equal('all');62 results.meta.pagination.pages.should.equal(1);63 results.tags.length.should.equal(5);64 done();65 }).catch(done);66 });67 it('with include post_count', function (done) {68 TagModel.findPage({limit: 'all', include: 'post_count'}).then(function (results) {69 results.meta.pagination.page.should.equal(1);70 results.meta.pagination.limit.should.equal('all');71 results.meta.pagination.pages.should.equal(1);72 results.tags.length.should.equal(5);73 should.exist(results.tags[0].post_count);74 done();75 }).catch(done);76 });77 });78 describe('findOne', function () {79 beforeEach(function (done) {80 testUtils.fixtures.insertPosts().then(function () {81 done();82 }).catch(done);83 });84 it('with slug', function (done) {85 var firstTag;86 TagModel.findPage().then(function (results) {87 should.exist(results);88 should.exist(results.tags);89 results.tags.length.should.be.above(0);90 firstTag = results.tags[0];91 return TagModel.findOne({slug: firstTag.slug});92 }).then(function (found) {93 should.exist(found);94 done();95 }).catch(done);96 });97 });98 describe('Post tag handling, post with NO tags', function () {99 var postJSON,100 tagJSON,101 editOptions,102 createTag = testUtils.DataGenerator.forKnex.createTag;103 beforeEach(function (done) {104 tagJSON = [];105 var post = testUtils.DataGenerator.forModel.posts[0],106 extraTag1 = createTag({name: 'existing tag a'}),107 extraTag2 = createTag({name: 'existing-tag-b'}),108 extraTag3 = createTag({name: 'existing_tag_c'});109 return Promise.props({110 post: PostModel.add(post, _.extend({}, context, {withRelated: ['tags']})),111 tag1: TagModel.add(extraTag1, context),112 tag2: TagModel.add(extraTag2, context),113 tag3: TagModel.add(extraTag3, context)114 }).then(function (result) {115 postJSON = result.post.toJSON({include: ['tags']});116 tagJSON.push(result.tag1.toJSON());117 tagJSON.push(result.tag2.toJSON());118 tagJSON.push(result.tag3.toJSON());119 editOptions = _.extend({}, context, {id: postJSON.id, withRelated: ['tags']});120 done();121 }).catch(done);122 });123 it('should create the test data correctly', function () {124 // creates two test tags125 should.exist(tagJSON);126 tagJSON.should.be.an.Array.with.lengthOf(3);127 tagJSON.should.have.enumerable(0).with.property('name', 'existing tag a');128 tagJSON.should.have.enumerable(1).with.property('name', 'existing-tag-b');129 tagJSON.should.have.enumerable(2).with.property('name', 'existing_tag_c');130 // creates a test post with no tags131 should.exist(postJSON);132 postJSON.title.should.eql('HTML Ipsum');133 should.exist(postJSON.tags);134 });135 describe('Adding brand new tags', function () {136 it('can add a single tag', function (done) {137 var newJSON = _.cloneDeep(postJSON);138 // Add a single tag to the end of the array139 newJSON.tags.push(createTag({name: 'tag1'}));140 // Edit the post141 return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {142 updatedPost = updatedPost.toJSON({include: ['tags']});143 updatedPost.tags.should.have.lengthOf(1);144 updatedPost.tags.should.have.enumerable(0).with.property('name', 'tag1');145 done();146 }).catch(done);147 });148 it('can add multiple tags', function (done) {149 var newJSON = _.cloneDeep(postJSON);150 // Add a bunch of tags to the end of the array151 newJSON.tags.push(createTag({name: 'tag1'}));152 newJSON.tags.push(createTag({name: 'tag2'}));153 newJSON.tags.push(createTag({name: 'tag3'}));154 newJSON.tags.push(createTag({name: 'tag4'}));155 newJSON.tags.push(createTag({name: 'tag5'}));156 // Edit the post157 return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {158 updatedPost = updatedPost.toJSON({include: ['tags']});159 updatedPost.tags.should.have.lengthOf(5);160 updatedPost.tags.should.have.enumerable(0).with.property('name', 'tag1');161 updatedPost.tags.should.have.enumerable(1).with.property('name', 'tag2');162 updatedPost.tags.should.have.enumerable(2).with.property('name', 'tag3');163 updatedPost.tags.should.have.enumerable(3).with.property('name', 'tag4');164 updatedPost.tags.should.have.enumerable(4).with.property('name', 'tag5');165 done();166 }).catch(done);167 });168 it('can add multiple tags with conflicting slugs', function (done) {169 var newJSON = _.cloneDeep(postJSON);170 // Add conflicting tags to the end of the array171 newJSON.tags.push({name: 'C'});172 newJSON.tags.push({name: 'C++'});173 newJSON.tags.push({name: 'C#'});174 // Edit the post175 return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {176 updatedPost = updatedPost.toJSON({include: ['tags']});177 updatedPost.tags.should.have.lengthOf(3);178 updatedPost.tags.should.have.enumerable(0).with.properties({name: 'C', slug: 'c'});179 updatedPost.tags.should.have.enumerable(1).with.properties({name: 'C++', slug: 'c-2'});180 updatedPost.tags.should.have.enumerable(2).with.properties({name: 'C#', slug: 'c-3'});181 done();182 }).catch(done);183 });184 });185 describe('Adding pre-existing tags', function () {186 it('can add a single tag', function (done) {187 var newJSON = _.cloneDeep(postJSON);188 // Add a single pre-existing tag189 newJSON.tags.push(tagJSON[0]);190 // Edit the post191 return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {192 updatedPost = updatedPost.toJSON({include: ['tags']});193 updatedPost.tags.should.have.lengthOf(1);194 updatedPost.tags.should.have.enumerable(0).with.properties({name: 'existing tag a', id: tagJSON[0].id});195 done();196 }).catch(done);197 });198 it('can add multiple tags', function (done) {199 var newJSON = _.cloneDeep(postJSON);200 // Add many preexisting tags201 newJSON.tags.push(tagJSON[0]);202 newJSON.tags.push(tagJSON[1]);203 newJSON.tags.push(tagJSON[2]);204 // Edit the post205 return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {206 updatedPost = updatedPost.toJSON({include: ['tags']});207 updatedPost.tags.should.have.lengthOf(3);208 updatedPost.tags.should.have.enumerable(0).with.properties({name: 'existing tag a', id: tagJSON[0].id});209 updatedPost.tags.should.have.enumerable(1).with.properties({name: 'existing-tag-b', id: tagJSON[1].id});210 updatedPost.tags.should.have.enumerable(2).with.properties({name: 'existing_tag_c', id: tagJSON[2].id});211 done();212 }).catch(done);213 });214 it('can add multiple tags in wrong order', function (done) {215 var newJSON = _.cloneDeep(postJSON);216 // Add tags to the array217 newJSON.tags.push(tagJSON[2]);218 newJSON.tags.push(tagJSON[0]);219 newJSON.tags.push(tagJSON[1]);220 // Edit the post221 return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {222 updatedPost = updatedPost.toJSON({include: ['tags']});223 updatedPost.tags.should.have.lengthOf(3);224 updatedPost.tags.should.have.enumerable(0).with.properties({name: 'existing_tag_c', id: tagJSON[2].id});225 updatedPost.tags.should.have.enumerable(1).with.properties({name: 'existing tag a', id: tagJSON[0].id});226 updatedPost.tags.should.have.enumerable(2).with.properties({name: 'existing-tag-b', id: tagJSON[1].id});227 done();228 }).catch(done);229 });230 });231 describe('Adding combinations', function () {232 it('can add a combination of new and pre-existing tags', function (done) {233 var newJSON = _.cloneDeep(postJSON);234 // Add a bunch of new and existing tags to the array235 newJSON.tags.push({name: 'tag1'});236 newJSON.tags.push({name: 'existing tag a'});237 newJSON.tags.push({name: 'tag3'});238 newJSON.tags.push({name: 'existing-tag-b'});239 newJSON.tags.push({name: 'tag5'});240 newJSON.tags.push({name: 'existing_tag_c'});241 // Edit the post242 return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {243 updatedPost = updatedPost.toJSON({include: ['tags']});244 updatedPost.tags.should.have.lengthOf(6);245 updatedPost.tags.should.have.enumerable(0).with.property('name', 'tag1');246 updatedPost.tags.should.have.enumerable(1).with.properties({name: 'existing tag a', id: tagJSON[0].id});247 updatedPost.tags.should.have.enumerable(2).with.property('name', 'tag3');248 updatedPost.tags.should.have.enumerable(3).with.properties({name: 'existing-tag-b', id: tagJSON[1].id});249 updatedPost.tags.should.have.enumerable(4).with.property('name', 'tag5');250 updatedPost.tags.should.have.enumerable(5).with.properties({name: 'existing_tag_c', id: tagJSON[2].id});251 done();252 }).catch(done);253 });254 });255 });256 describe('Post tag handling, post with tags', function () {257 var postJSON,258 tagJSON,259 editOptions,260 createTag = testUtils.DataGenerator.forKnex.createTag;261 beforeEach(function (done) {262 tagJSON = [];263 var post = testUtils.DataGenerator.forModel.posts[0],264 postTags = [265 createTag({name: 'tag1'}),266 createTag({name: 'tag2'}),267 createTag({name: 'tag3'})268 ],269 extraTags = [270 createTag({name: 'existing tag a'}),271 createTag({name: 'existing-tag-b'}),272 createTag({name: 'existing_tag_c'})273 ];274 post.tags = postTags;275 return Promise.props({276 post: PostModel.add(post, _.extend({}, context, {withRelated: ['tags']})),277 tag1: TagModel.add(extraTags[0], context),278 tag2: TagModel.add(extraTags[1], context),279 tag3: TagModel.add(extraTags[2], context)280 }).then(function (result) {281 postJSON = result.post.toJSON({include: ['tags']});282 tagJSON.push(result.tag1.toJSON());283 tagJSON.push(result.tag2.toJSON());284 tagJSON.push(result.tag3.toJSON());285 editOptions = _.extend({}, context, {id: postJSON.id, withRelated: ['tags']});286 done();287 });288 });289 it('should create the test data correctly', function () {290 // creates a test tag291 should.exist(tagJSON);292 tagJSON.should.be.an.Array.with.lengthOf(3);293 tagJSON.should.have.enumerable(0).with.property('name', 'existing tag a');294 tagJSON.should.have.enumerable(1).with.property('name', 'existing-tag-b');295 tagJSON.should.have.enumerable(2).with.property('name', 'existing_tag_c');296 // creates a test post with an array of tags in the correct order297 should.exist(postJSON);298 postJSON.title.should.eql('HTML Ipsum');299 should.exist(postJSON.tags);300 postJSON.tags.should.be.an.Array.and.have.lengthOf(3);301 postJSON.tags.should.have.enumerable(0).with.property('name', 'tag1');302 postJSON.tags.should.have.enumerable(1).with.property('name', 'tag2');303 postJSON.tags.should.have.enumerable(2).with.property('name', 'tag3');304 });305 describe('Adding brand new tags', function () {306 it('can add a single tag to the end of the tags array', function (done) {307 var newJSON = _.cloneDeep(postJSON);308 // Add a single tag to the end of the array309 newJSON.tags.push(createTag({name: 'tag4'}));310 // Edit the post311 return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {312 updatedPost = updatedPost.toJSON({include: ['tags']});313 updatedPost.tags.should.have.lengthOf(4);314 updatedPost.tags.should.have.enumerable(0).with.properties({name: 'tag1', id: postJSON.tags[0].id});315 updatedPost.tags.should.have.enumerable(1).with.properties({name: 'tag2', id: postJSON.tags[1].id});316 updatedPost.tags.should.have.enumerable(2).with.properties({name: 'tag3', id: postJSON.tags[2].id});317 updatedPost.tags.should.have.enumerable(3).with.property('name', 'tag4');318 done();319 }).catch(done);320 });321 it('can add a single tag to the beginning of the tags array', function (done) {322 var newJSON = _.cloneDeep(postJSON);323 // Add a single tag to the beginning of the array324 newJSON.tags = [createTag({name: 'tag4'})].concat(postJSON.tags);325 // Edit the post326 return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {327 updatedPost = updatedPost.toJSON({include: ['tags']});328 updatedPost.tags.should.have.lengthOf(4);329 updatedPost.tags.should.have.enumerable(0).with.property('name', 'tag4');330 updatedPost.tags.should.have.enumerable(1).with.properties({name: 'tag1', id: postJSON.tags[0].id});331 updatedPost.tags.should.have.enumerable(2).with.properties({name: 'tag2', id: postJSON.tags[1].id});332 updatedPost.tags.should.have.enumerable(3).with.properties({name: 'tag3', id: postJSON.tags[2].id});333 done();334 }).catch(done);335 });336 });337 describe('Adding pre-existing tags', function () {338 it('can add a single tag to the end of the tags array', function (done) {339 var newJSON = _.cloneDeep(postJSON);340 // Add a single pre-existing tag to the end of the array341 newJSON.tags.push(tagJSON[0]);342 // Edit the post343 return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {344 updatedPost = updatedPost.toJSON({include: ['tags']});345 updatedPost.tags.should.have.lengthOf(4);346 updatedPost.tags.should.have.enumerable(0).with.properties({name: 'tag1', id: postJSON.tags[0].id});347 updatedPost.tags.should.have.enumerable(1).with.properties({name: 'tag2', id: postJSON.tags[1].id});348 updatedPost.tags.should.have.enumerable(2).with.properties({name: 'tag3', id: postJSON.tags[2].id});349 updatedPost.tags.should.have.enumerable(3).with.properties({name: 'existing tag a', id: tagJSON[0].id});350 done();351 }).catch(done);352 });353 it('can add a single tag to the beginning of the tags array', function (done) {354 var newJSON = _.cloneDeep(postJSON);355 // Add an existing tag to the beginning of the array356 newJSON.tags = [tagJSON[0]].concat(postJSON.tags);357 // Edit the post358 return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {359 updatedPost = updatedPost.toJSON({include: ['tags']});360 updatedPost.tags.should.have.lengthOf(4);361 updatedPost.tags.should.have.enumerable(0).with.properties({name: 'existing tag a', id: tagJSON[0].id});362 updatedPost.tags.should.have.enumerable(1).with.properties({name: 'tag1', id: postJSON.tags[0].id});363 updatedPost.tags.should.have.enumerable(2).with.properties({name: 'tag2', id: postJSON.tags[1].id});364 updatedPost.tags.should.have.enumerable(3).with.properties({name: 'tag3', id: postJSON.tags[2].id});365 done();366 }).catch(done);367 });368 it('can add a single tag to the middle of the tags array', function (done) {369 var newJSON = _.cloneDeep(postJSON);370 // Add a single pre-existing tag to the middle of the array371 newJSON.tags = postJSON.tags.slice(0, 1).concat([tagJSON[0]]).concat(postJSON.tags.slice(1));372 // Edit the post373 return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {374 updatedPost = updatedPost.toJSON({include: ['tags']});375 updatedPost.tags.should.have.lengthOf(4);376 updatedPost.tags.should.have.enumerable(0).with.properties({name: 'tag1', id: postJSON.tags[0].id});377 updatedPost.tags.should.have.enumerable(1).with.properties({name: 'existing tag a', id: tagJSON[0].id});378 updatedPost.tags.should.have.enumerable(2).with.properties({name: 'tag2', id: postJSON.tags[1].id});379 updatedPost.tags.should.have.enumerable(3).with.properties({name: 'tag3', id: postJSON.tags[2].id});380 done();381 }).catch(done);382 });383 });384 describe('Removing tags', function () {385 it('can remove a single tag from the end of the tags array', function (done) {386 var newJSON = _.cloneDeep(postJSON);387 // Remove a single tag from the end of the array388 newJSON.tags = postJSON.tags.slice(0, -1);389 // Edit the post390 return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {391 updatedPost = updatedPost.toJSON({include: ['tags']});392 updatedPost.tags.should.have.lengthOf(2);393 updatedPost.tags.should.have.enumerable(0).with.properties({name: 'tag1', id: postJSON.tags[0].id});394 updatedPost.tags.should.have.enumerable(1).with.properties({name: 'tag2', id: postJSON.tags[1].id});395 done();396 }).catch(done);397 });398 it('can remove a single tag from the beginning of the tags array', function (done) {399 var newJSON = _.cloneDeep(postJSON);400 // Remove a single tag from the beginning of the array401 newJSON.tags = postJSON.tags.slice(1);402 // Edit the post403 return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {404 updatedPost = updatedPost.toJSON({include: ['tags']});405 updatedPost.tags.should.have.lengthOf(2);406 updatedPost.tags.should.have.enumerable(0).with.properties({name: 'tag2', id: postJSON.tags[1].id});407 updatedPost.tags.should.have.enumerable(1).with.properties({name: 'tag3', id: postJSON.tags[2].id});408 done();409 }).catch(done);410 });411 it('can remove all tags', function (done) {412 var newJSON = _.cloneDeep(postJSON);413 // Remove all the tags414 newJSON.tags = [];415 // Edit the post416 return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {417 updatedPost = updatedPost.toJSON({include: ['tags']});418 updatedPost.tags.should.have.lengthOf(0);419 done();420 }).catch(done);421 });422 });423 describe('Reordering tags', function () {424 it('can reorder the first tag to be the last', function (done) {425 var newJSON = _.cloneDeep(postJSON),426 firstTag = [postJSON.tags[0]];427 // Reorder the tags, so that the first tag is moved to the end428 newJSON.tags = postJSON.tags.slice(1).concat(firstTag);429 // Edit the post430 return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {431 updatedPost = updatedPost.toJSON({include: ['tags']});432 updatedPost.tags.should.have.lengthOf(3);433 updatedPost.tags.should.have.enumerable(0).with.properties({name: 'tag2', id: postJSON.tags[1].id});434 updatedPost.tags.should.have.enumerable(1).with.properties({name: 'tag3', id: postJSON.tags[2].id});435 updatedPost.tags.should.have.enumerable(2).with.properties({name: 'tag1', id: postJSON.tags[0].id});436 done();437 }).catch(done);438 });439 it('can reorder the last tag to be the first', function (done) {440 var newJSON = _.cloneDeep(postJSON),441 lastTag = [postJSON.tags[2]];442 // Reorder the tags, so that the last tag is moved to the beginning443 newJSON.tags = lastTag.concat(postJSON.tags.slice(0, -1));444 // Edit the post445 return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {446 updatedPost = updatedPost.toJSON({include: ['tags']});447 updatedPost.tags.should.have.lengthOf(3);448 updatedPost.tags.should.have.enumerable(0).with.properties({name: 'tag3', id: postJSON.tags[2].id});449 updatedPost.tags.should.have.enumerable(1).with.properties({name: 'tag1', id: postJSON.tags[0].id});450 updatedPost.tags.should.have.enumerable(2).with.properties({name: 'tag2', id: postJSON.tags[1].id});451 done();452 }).catch(done);453 });454 });455 describe('Combination updates', function () {456 it('can add a combination of new and pre-existing tags', function (done) {457 var newJSON = _.cloneDeep(postJSON);458 // Push a bunch of new and existing tags to the end of the array459 newJSON.tags.push({name: 'tag4'});460 newJSON.tags.push({name: 'existing tag a'});461 newJSON.tags.push({name: 'tag5'});462 newJSON.tags.push({name: 'existing-tag-b'});463 newJSON.tags.push({name: 'bob'});464 newJSON.tags.push({name: 'existing_tag_c'});465 // Edit the post466 return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {467 updatedPost = updatedPost.toJSON({include: ['tags']});468 updatedPost.tags.should.have.lengthOf(9);469 updatedPost.tags.should.have.enumerable(0).with.properties({name: 'tag1', id: postJSON.tags[0].id});470 updatedPost.tags.should.have.enumerable(1).with.properties({name: 'tag2', id: postJSON.tags[1].id});471 updatedPost.tags.should.have.enumerable(2).with.properties({name: 'tag3', id: postJSON.tags[2].id});472 updatedPost.tags.should.have.enumerable(3).with.property('name', 'tag4');473 updatedPost.tags.should.have.enumerable(4).with.properties({name: 'existing tag a', id: tagJSON[0].id});474 updatedPost.tags.should.have.enumerable(5).with.property('name', 'tag5');475 updatedPost.tags.should.have.enumerable(6).with.properties({name: 'existing-tag-b', id: tagJSON[1].id});476 updatedPost.tags.should.have.enumerable(7).with.property('name', 'bob');477 updatedPost.tags.should.have.enumerable(8).with.properties({name: 'existing_tag_c', id: tagJSON[2].id});478 done();479 }).catch(done);480 });481 it('can reorder the first tag to be the last and add a tag to the beginning', function (done) {482 var newJSON = _.cloneDeep(postJSON),483 firstTag = [postJSON.tags[0]];484 // Add a new tag to the beginning, and move the original first tag to the end485 newJSON.tags = [tagJSON[0]].concat(postJSON.tags.slice(1)).concat(firstTag);486 // Edit the post487 return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {488 updatedPost = updatedPost.toJSON({include: ['tags']});489 updatedPost.tags.should.have.lengthOf(4);490 updatedPost.tags.should.have.enumerable(0).with.properties({name: 'existing tag a', id: tagJSON[0].id});491 updatedPost.tags.should.have.enumerable(1).with.properties({name: 'tag2', id: postJSON.tags[1].id});492 updatedPost.tags.should.have.enumerable(2).with.properties({name: 'tag3', id: postJSON.tags[2].id});493 updatedPost.tags.should.have.enumerable(3).with.properties({name: 'tag1', id: postJSON.tags[0].id});494 done();495 }).catch(done);496 });497 it('can reorder the first tag to be the last, remove the original last tag & add a tag to the beginning', function (done) {498 var newJSON = _.cloneDeep(postJSON),499 firstTag = [newJSON.tags[0]];500 // And an existing tag to the beginning of the array, move the original first tag to the end and remove the original last tag501 newJSON.tags = [tagJSON[0]].concat(newJSON.tags.slice(1, -1)).concat(firstTag);502 // Edit the post503 return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {504 updatedPost = updatedPost.toJSON({include: ['tags']});505 updatedPost.tags.should.have.lengthOf(3);506 updatedPost.tags.should.have.enumerable(0).with.properties({name: 'existing tag a', id: tagJSON[0].id});507 updatedPost.tags.should.have.enumerable(1).with.properties({name: 'tag2', id: postJSON.tags[1].id});508 updatedPost.tags.should.have.enumerable(2).with.properties({name: 'tag1', id: postJSON.tags[0].id});509 done();510 }).catch(done);511 });512 it('can reorder original tags, remove one, and add new and existing tags', function (done) {513 var newJSON = _.cloneDeep(postJSON),514 firstTag = [newJSON.tags[0]];515 // Reorder original 3 so that first is at the end516 newJSON.tags = newJSON.tags.slice(1).concat(firstTag);517 // add an existing tag in the middle518 newJSON.tags = newJSON.tags.slice(0, 1).concat({name: 'existing-tag-b'}).concat(newJSON.tags.slice(1));519 // add a brand new tag in the middle520 newJSON.tags = newJSON.tags.slice(0, 3).concat({name: 'betty'}).concat(newJSON.tags.slice(3));521 // Add some more tags to the end522 newJSON.tags.push({name: 'bob'});523 newJSON.tags.push({name: 'existing tag a'});524 // Edit the post525 return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {526 updatedPost = updatedPost.toJSON({include: ['tags']});527 updatedPost.tags.should.have.lengthOf(7);528 updatedPost.tags.should.have.enumerable(0).with.properties({name: 'tag2', id: postJSON.tags[1].id});529 updatedPost.tags.should.have.enumerable(1).with.properties({name: 'existing-tag-b', id: tagJSON[1].id});530 updatedPost.tags.should.have.enumerable(2).with.properties({name: 'tag3', id: postJSON.tags[2].id});531 updatedPost.tags.should.have.enumerable(3).with.property('name', 'betty');532 updatedPost.tags.should.have.enumerable(4).with.properties({name: 'tag1', id: postJSON.tags[0].id});533 updatedPost.tags.should.have.enumerable(5).with.property('name', 'bob');534 updatedPost.tags.should.have.enumerable(6).with.properties({name: 'existing tag a', id: tagJSON[0].id});535 done();536 }).catch(done);537 });538 });539 });...

Full Screen

Full Screen

jsl.format.js

Source:jsl.format.js Github

copy

Full Screen

1/*jslint white: true, devel: true, onevar: true, browser: true, undef: true, nomen: true, regexp: true, plusplus: false, bitwise: true, newcap: true, maxerr: 50, indent: 4 */2var jsl = typeof jsl === 'undefined' ? {} : jsl;3/**4 * jsl.format - Provide json reformatting in a character-by-character approach, so that even invalid JSON may be reformatted (to the best of its ability).5 *6**/7jsl.format = (function () {8 function repeat(s, count) {9 return new Array(count + 1).join(s);10 }11 function formatJson(json) {12 var i = 0,13 il = 0,14 tab = " ",15 newJson = "",16 indentLevel = 0,17 inString = false,18 currentChar = null;19 for (i = 0, il = json.length; i < il; i += 1) { 20 currentChar = json.charAt(i);21 switch (currentChar) {22 case '{': 23 case '[': 24 if (!inString) { 25 newJson += currentChar + "\n" + repeat(tab, indentLevel + 1);26 indentLevel += 1; 27 } else { 28 newJson += currentChar; 29 }30 break; 31 case '}': 32 case ']': 33 if (!inString) { 34 indentLevel -= 1; 35 newJson += "\n" + repeat(tab, indentLevel) + currentChar; 36 } else { 37 newJson += currentChar; 38 } 39 break; 40 case ',': 41 if (!inString) { 42 newJson += ",\n" + repeat(tab, indentLevel); 43 } else { 44 newJson += currentChar; 45 } 46 break; 47 case ':': 48 if (!inString) { 49 newJson += ": "; 50 } else { 51 newJson += currentChar; 52 } 53 break; 54 case ' ':55 case "\n":56 case "\t":57 if (inString) {58 newJson += currentChar;59 }60 break;61 case '"': 62 if (i > 0 && json.charAt(i - 1) !== '\\') {63 inString = !inString; 64 }65 newJson += currentChar; 66 break;67 default: 68 newJson += currentChar; 69 break; 70 } 71 } 72 return newJson; 73 }74 return { "formatJson": formatJson };...

Full Screen

Full Screen

JsonFormatter.js

Source:JsonFormatter.js Github

copy

Full Screen

1/*jslint white: true, devel: true, onevar: true, browser: true, undef: true, nomen: true, regexp: true, plusplus: false, bitwise: true, newcap: true, maxerr: 50, indent: 4 */2var jsl = typeof jsl === 'undefined' ? {} : jsl;3/**4 * jsl.format - Provide json reformatting in a character-by-character approach, so that even invalid JSON may be reformatted (to the best of its ability).5 *6**/7jsl.format = (function () {8 function repeat(s, count) {9 return new Array(count + 1).join(s);10 }11 function formatJson(json) {12 var i = 0,13 il = 0,14 tab = " ",15 newJson = "",16 indentLevel = 0,17 inString = false,18 currentChar = null;19 for (i = 0, il = json.length; i < il; i += 1) { 20 currentChar = json.charAt(i);21 switch (currentChar) {22 case '{': 23 case '[': 24 if (!inString) { 25 newJson += currentChar + "\n" + repeat(tab, indentLevel + 1);26 indentLevel += 1; 27 } else { 28 newJson += currentChar; 29 }30 break; 31 case '}': 32 case ']': 33 if (!inString) { 34 indentLevel -= 1; 35 newJson += "\n" + repeat(tab, indentLevel) + currentChar; 36 } else { 37 newJson += currentChar; 38 } 39 break; 40 case ',': 41 if (!inString) { 42 newJson += ",\n" + repeat(tab, indentLevel); 43 } else { 44 newJson += currentChar; 45 } 46 break; 47 case ':': 48 if (!inString) { 49 newJson += ": "; 50 } else { 51 newJson += currentChar; 52 } 53 break; 54 case ' ':55 case "\n":56 case "\t":57 if (inString) {58 newJson += currentChar;59 }60 break;61 case '"': 62 if (i > 0 && json.charAt(i - 1) !== '\\') {63 inString = !inString; 64 }65 newJson += currentChar; 66 break;67 default: 68 newJson += currentChar; 69 break; 70 } 71 } 72 return newJson; 73 }74 return { "formatJson": formatJson };...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var argosy = require('argosy')2var argosyPattern = require('argosy-pattern')3var argosyJson = require('argosy-json')4var argosyService = argosy()5 .use(argosyJson())6 .use(argosyPattern({7 }))8 .act('get:hello', function (err, result) {9 console.log(result)10 })11argosyService.listen(8000)12var argosy = require('argosy')13var argosyPattern = require('argosy-pattern')14var argosyJson = require('argosy-json')15var argosyClient = argosy()16 .use(argosyJson())17 .use(argosyPattern({18 }))19 .act('get:hello', function (err, result) {20 console.log(result)21 })22argosyClient.connect(8000)23Error: No handler found for {"get":"hello"}

Full Screen

Using AI Code Generation

copy

Full Screen

1var argosy = require('argosy')2var argosyPattern = require('argosy-pattern')3var argosyJson = require('argosy-json')4var argosyPipe = require('argosy-pipe')5var argosyHttp = require('argosy-http')6var pipeline = argosy()7pipeline.use(argosyPattern({8 'test': { }9}))10pipeline.use(argosyJson())11pipeline.use(argosyPipe())12pipeline.use(argosyHttp())13pipeline.listen(8000)14var argosy = require('argosy')15var argosyPattern = require('argosy-pattern')16var argosyJson = require('argosy-json')17var argosyPipe = require('argosy-pipe')18var argosyHttp = require('argosy-http')19var pipeline = argosy()20pipeline.use(argosyPattern({21 'test': { }22}))23pipeline.use(argosyJson())24pipeline.use(argosyPipe())25pipeline.use(argosyHttp())26pipeline.listen(8000)27var argosy = require('argosy')28var argosyPattern = require('argosy-pattern')29var argosyJson = require('argosy-json')30var argosyPipe = require('argosy-pipe')31var argosyHttp = require('argosy-http')32var pipeline = argosy()33pipeline.use(argosyPattern({34 'test': { }35}))36pipeline.use(argosyJson())37pipeline.use(argosyPipe())38pipeline.use(argosyHttp())39pipeline.listen(8000)40var argosy = require('argosy')41var argosyPattern = require('argosy-pattern')42var argosyJson = require('argosy-json')43var argosyPipe = require('argosy-pipe')44var argosyHttp = require('argosy-http')45var pipeline = argosy()46pipeline.use(argosyPattern({47 'test': { }48}))49pipeline.use(argosyJson())50pipeline.use(argosyPipe())51pipeline.use(argosyHttp())52pipeline.listen(8000)

Full Screen

Using AI Code Generation

copy

Full Screen

1var argosy = require('argosy')2var pattern = require('argosy-pattern')3var newJson = require('argosy-new-json')4var service = argosy()5service.pipe(newJson()).pipe(service)6service.accept({hello: pattern.match.string})7service.on('hello', function (msg, respond) {8 respond(null, {message: 'Hello ' + msg})9})10service.act({hello: 'world'}, console.log)11var argosy = require('argosy')12var pattern = require('argosy-pattern')13var newJson = require('argosy-new-json')14var service = argosy()15service.pipe(newJson()).pipe(service)16service.accept({hello: pattern.match.string})17service.on('hello', function (msg, respond) {18 respond(null, {message: 'Hello ' + msg})19})20service.act({hello: 'world'}, console.log)21var argosy = require('argosy')22var pattern = require('argosy-pattern')23var newJson = require('argosy-new-json')24var service = argosy()25service.pipe(newJson()).pipe(service)26service.accept({hello: pattern.match.string})27service.on('hello', function (msg, respond) {28 respond(null, {message: 'Hello ' + msg})29})30service.act({hello: 'world'}, console.log)31var argosy = require('argosy')32var pattern = require('argosy-pattern')33var newJson = require('argosy-new-json')34var service = argosy()35service.pipe(newJson()).pipe(service)36service.accept({hello: pattern.match.string})37service.on('hello', function (msg, respond) {38 respond(null, {message: 'Hello ' + msg})39})40service.act({hello: 'world'}, console.log)41var argosy = require('argosy')42var pattern = require('argosy-pattern')43var newJson = require('argosy-new-json')44var service = argosy()45service.pipe(newJson()).pipe(service)46service.accept({hello: pattern.match.string})

Full Screen

Using AI Code Generation

copy

Full Screen

1var argosy = require('argosy')2var argosyPatter = require('argosy-pattern')3var argosyJson = require('argosy-json')4var service = argosy()5service.use(argosyPatter())6service.use(argosyJson())7service.accept({role: 'test', cmd: 'newJson'}, function (msg, cb) {8 cb(null, {foo: 'bar'})9})10service.listen(5000, function () {11 var client = argosy()12 client.use(argosyPatter())13 client.use(argosyJson())14 client.connect(5000)15 client.act({role: 'test', cmd: 'newJson'}, function (err, response) {16 console.log(response)17 })18})19var argosy = require('argosy')20var argosyPatter = require('argosy-pattern')21var argosyJson = require('argosy-json')22var service = argosy()23service.use(argosyPatter())24service.use(argosyJson())25service.accept({role: 'test', cmd: 'json'}, function (msg, cb) {26 cb(null, {foo: 'bar'})27})28service.listen(5000, function () {29 var client = argosy()30 client.use(argosyPatter())31 client.use(argosyJson())32 client.connect(5000)33 client.act({role: 'test', cmd: 'json'}, function (err, response) {34 console.log(response)35 })36})

Full Screen

Using AI Code Generation

copy

Full Screen

1var argosy = require('argosy')2var argosyPattern = require('argosy-pattern')3var argosyService = argosy()4var pattern = argosyPattern({5})6argosyService.accept(pattern).process(function (msg, respond) {7 console.log('newJson', msg.newJson)8 respond(null, msg.newJson)9})10argosyService.pipe(argosyService)11var argosy = require('argosy')12var argosyPattern = require('argosy-pattern')13var argosyService = argosy()14var pattern = argosyPattern({15})16argosyService.accept(pattern).process(function (msg, respond) {17 console.log('newJson', msg.newJson)18 respond(null, msg.newJson)19})20argosyService.pipe(argosyService)21var argosy = require('argosy')22var argosyService = argosy()23var argosyPattern = require('argosy-pattern')24var pattern = argosyPattern({25})26var argosyService2 = argosy()27var argosyPattern2 = require('argosy-pattern')28var pattern2 = argosyPattern2({29})30 .accept(pattern)31 .process(function (msg, respond) {32 console.log('newJson', msg.newJson)33 respond(null, msg.newJson)34 })35 .pipe(argosyService2)36 .accept(pattern2)37 .process(function (msg, respond) {38 console.log('newJson', msg.newJson)39 respond(null, msg.newJson)40 })41 .pipe(argosyService)42var argosy = require('argosy')43var argosyPattern = require('argosy-pattern')44var argosyService = argosy()45var pattern = argosyPattern({46})47argosyService.accept(pattern).process(function (msg, respond) {48 console.log('newJson', msg.newJson)49 respond(null, msg

Full Screen

Using AI Code Generation

copy

Full Screen

1var service = require('argosy-service')2var argosy = require('argosy')3var seneca = require('seneca')()4module.exports = function () {5 return seneca.use('..', {6 service: {7 }8 })9}10var service = require('argosy-service')11var argosy = require('argosy')12var seneca = require('seneca')()13module.exports = function () {14 return seneca.use('..', {15 service: {16 }17 })18}19var service = require('argosy-service')20var argosy = require('argosy')21var seneca = require('seneca')()22module.exports = function () {23 return seneca.use('..', {24 service: {25 }26 })27}28var service = require('argosy-service')29var argosy = require('argosy')30var seneca = require('seneca')()31module.exports = function () {32 return seneca.use('..', {33 service: {34 }35 })36}37var service = require('argosy-service')38var argosy = require('argosy')39var seneca = require('seneca')()40module.exports = function () {41 return seneca.use('..', {42 service: {43 }44 })45}46var service = require('argosy-service')47var argosy = require('argosy')48var seneca = require('seneca')()49module.exports = function () {50 return seneca.use('

Full Screen

Using AI Code Generation

copy

Full Screen

1var argosy = require('argosy')2var argosyPattern = require('argosy-pattern')3var argosyService = argosy()4var pattern = argosyPattern({5})6argosyService.accept(pattern).process(function (msg, respond) {7 console.log('newJson', msg.newJson)8 respond(null, msg.newJson)9})10argosyService.pipe(argosyService)11var argosy = require('argosy')12var argosyPattern = require('argosy-pattern')13var argosyService = argosy()14var pattern = argosyPattern({15})16argosyService.accept(pattern).process(function (msg, respond) {17 console.log('newJson', msg.newJson)18 respond(null, msg.newJson)19})20argosyService.pipe(argosyService)21var argosy = require('argosy')22var argosyService = argosy()23var argosyPattern = require('argosy-pattern')24var pattern = argosyPattern({25})26var argosyService2 = argosy()27var argosyPattern2 = require('argosy-pattern')28var pattern2 = argosyPattern2({29})30 .accept(pattern)31 .process(function (msg, respond) {32 console.log('newJson', msg.newJson)33 respond(null, msg.newJson)34 })35 .pipe(argosyService2)36 .accept(pattern2)37 .process(function (msg, respond) {38 console.log('newJson', msg.newJson)39 respond(null, msg.newJson)40 })41 .pipe(argosyService)42var argosy = require('argosy')43var argosyPattern = require('argosy-pattern')44var argosyService = argosy()45var pattern = argosyPattern({46})47argosyService.accept(pattern).process(function (msg, respond) {48 console.log('newJson', msg.newJson)49 respond(null, msg

Full Screen

Using AI Code Generation

copy

Full Screen

1const argosyPattern = require('argosy-pattern')2const pattern = newJson('hello', { name: String })3const argosyPattern = require('argosy-pattern')4const pattern = newJson('hello', { name: String })5const argosyPattern = require('argosy-pattern')6const pattern = newJson('hello', { namePath: test4.js7var argosy = require('argosy')8var pattern = require('argosy-pattern')9var newJson = require('argosy-new-json')10var service = argosy()11service.pipe(newJson()).pipe(service)12service.accept({hello: pattern.match.string})13service.on('hello', function (msg, respond) {14 respond(null, {message: 'Hello ' + msg})15})16service.act({hello: 'world'}, console.log)17var argosy = require('argosy')18var pattern = require('argosy-pattern')19var newJson = require('argosy-new-json')20var service = argosy()21service.pipe(newJson()).pipe(service)22service.accept({hello: pattern.match.string})

Full Screen

Using AI Code Generation

copy

Full Screen

1var argosy = require('argosy')2var argosyPatter = require('argosy-pattern')3var argosyJson = require('argosy-json')4var service = argosy()5service.use(argosyPatter())6service.use(argosyJson())7service.accept({role: 'test', cmd: 'newJson'}, function (msg, cb) {8 cb(null, {foo: 'bar'})9})10service.listen(5000, function () {11 var client = argosy()12 client.use(argosyPatter())13 client.use(argosyJson())14 client.connect(5000)15 client.act({role: 'test', cmd: 'newJson'}, function (err, response) {16 console.log(response)17 })18})19var argosy = require('argosy')20var argosyPatter = require('argosy-pattern')21var argosyJson = require('argosy-json')22var service = argosy()23service.use(argosyPatter())24service.use(argosyJson())25service.accept({role: 'test', cmd: 'json'}, function (msg, cb) {26 cb(null, {foo: 'bar'})27})28service.listen(5000, function () {29 var client = argosy()30 client.use(argosyPatter())31 client.use(argosyJson())32 client.connect(5000)33 client.act({role: 'test', cmd: 'json'}, function (err, response) {34 console.log(response)35 })36})

Full Screen

Using AI Code Generation

copy

Full Screen

1var argosyService = require('argosy-service').newJson;2var argosy = require('argosy');3var argosyPattern = require('argosy-pattern');4var service = argosyService({ 5 add: argosyPattern.accepts({6 }, {7 })8});9service.add(function (message, callback) {10 callback(null, { result: message.a + message.b });11});12argosy()13 .use('math', service)14 .use(function (req, res) {15 if (req.math) {16 req.math.add({ a: 2, b: 3 }, function (err, response) {17 });18 }19 })20 .listen(8000);

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run argos automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful