Best JavaScript code snippet using mountebank
model_tags_spec.js
Source:model_tags_spec.js
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 count.posts if include count.posts', function (done) {44 testUtils.fixtures.insertPosts().then(function () {45 TagModel.findOne({slug: 'kitchen-sink'}, {include: 'count.posts'}).then(function (tag) {46 should.exist(tag);47 tag.toJSON().count.posts.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 count.posts', function (done) {68 TagModel.findPage({limit: 'all', include: 'count.posts'}).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].count.posts);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 });...
index.js
Source:index.js
1import { post, postjson, postform } from '../../utils/base';2export default {3 //è®ç»è¥æ ç¾å表4 selectCampTypeList(param) {5 let reqdata = param ? param : {};6 return postjson('/campType/selectCampTypeList', reqdata, {})7 },8 //è®ç»è¥æ ç¾å表2 ååç¨ç9 selectCampTypeList2(param) {10 let reqdata = param ? param : {};11 return postjson('/campType/selectCampTypeList2', reqdata, {})12 },13 //è®ç»è¥æ ç¾å表3 课ç¨ç¨ç14 selectCampTypeList3(param) {15 let reqdata = param ? param : {};16 return postjson('/campType/selectCampTypeList3', reqdata, {})17 },18 //æ°å»ºæè
ä¿®æ¹æ ç¾19 campType_insertOrUpdate(param) {20 let reqdata = param ? param : {};21 return postjson('/campType/insertOrUpdate', reqdata, {})22 },23 //æ°å»ºæè
ä¿®æ¹æ ç¾24 25 campClass_nameChange(param) {26 let reqdata = param ? param : {};27 return postjson('/trainingCampLog/updateTrainingCampName', reqdata, {})28 },29 //å é¤è®ç»è¥æ ç¾30 campType_deleteById(param) {31 let reqdata = param ? param : {};32 return postjson('/campType/deleteById', reqdata, {})33 },34 // å建è®ç»è¥éæ©æ¨¡æ¿å表35 trainingCampLog_selectCreateTemplateList(param) {36 let reqdata = param ? param : {};37 return postjson('/trainingCampLog/selectCreateTemplateList', reqdata, {})38 },39 // è®ç»è¥ä¸ä¸æ¶40 trainingCampLog_updateCampStatus2(param) {41 let reqdata = param ? param : {};42 return postjson('/trainingCampLog/updateCampStatus2', reqdata, {})43 },44 //è®ç»è¥å¾çä¸ä¼ 45 trainingCampLog_insertPictures(param) {46 let reqdata = param ? param : {};47 return postform('/trainingCampLog/insertPictures', reqdata, {})48 },49 // è®ç»è¥å é¤å¾ç50 trainingCampLog_deleteImg(param) {51 let reqdata = param ? param : {};52 return post('/trainingCampLog/deleteImg', reqdata, {})53 },54 // æ°å»ºæä¿®æ¹è®ç»è¥55 trainingCampLog_createOrUpdateTrainingCamp(param) {56 let reqdata = param ? param : {};57 return postjson('/trainingCampLog/createOrUpdateTrainingCamp', reqdata, {})58 },59 // æ¥è¯¢è®ç»è¥è¯¦æ
60 trainingCampLog_selectCampManagement(param) {61 let reqdata = param ? param : {};62 return postjson('/trainingCampLog/selectCampManagement', reqdata, {})63 },64 // æ¥è¯¢è®ç»è¥çä»»å¡å表65 trainingCampLog_selectCampTaskList(param) {66 let reqdata = param ? param : {};67 return postjson('/trainingCampLog/selectCampTaskList', reqdata, {})68 },69 // å建æä¿®æ¹è®ç»è¥ä»»å¡70 trainingCampLog_insertOrUpdateCampTask(param) {71 let reqdata = param ? param : {};72 return postjson('/trainingCampLog/insertOrUpdateCampTask', reqdata, {})73 },74 // æ°å¢æä¿®æ¹ä»»å¡ä¸è§é¢75 trainingCampLog_insertTasksDetail(param) {76 let reqdata = param ? param : {};77 return postjson('/trainingCampLog/insertTasksDetail', reqdata, {})78 },79 // è·åè§é¢å表 ä»»å¡éæ©80 trainingCampLog_selectVideoLibrary(param) {81 let reqdata = param ? param : {};82 return postjson('/trainingCampLog/selectVideoLibrary', reqdata, {})83 },84 // è·åè§é¢å表 èµæºåº85 video_selectVideoLibraryList(param) {86 let reqdata = param ? param : {};87 return postjson('/video/selectVideoLibraryList', reqdata, {})88 },89 // æ°å¢æè
ä¿®æ¹ä»»å¡ä¸ä½ä¸90 trainingCampLog_insertcoursewareList(param) {91 let reqdata = param ? param : {};92 return postjson('/trainingCampLog/insertCampCourseWare', reqdata, {})93 },94 // ä½ä¸æ¨¡æ¿å表95 homeworkDocument_selectHomeworkTemplate(param) {96 let reqdata = param ? param : {};97 return postjson('/homeworkDocument/selectHomeworkTemplate', reqdata, {})98 },99 // æ°å¢æè
ä¿®æ¹ä»»å¡ä¸ä½ä¸100 trainingCampLog_insertTasksHomework(param) {101 let reqdata = param ? param : {};102 return postjson('/trainingCampLog/insertTasksHomework', reqdata, {})103 },104 // å é¤ä»»å¡ä¸ä½ä¸105 trainingCampLog_deleteCampHomeworkInfo(param) {106 let reqdata = param ? param : {};107 return postjson('/trainingCampLog/deleteCampHomeworkInfo', reqdata, {})108 },109 // å é¤è®ç»è¥ä¸ä»»å¡110 trainingCampLog_deleteCampTask(param) {111 let reqdata = param ? param : {};112 return postjson('/trainingCampLog/deleteCampTask', reqdata, {})113 },114 // è®ç»è¥æ¨¡æ¿å表115 trainingCampLog_selectCampTemplateList(param) {116 let reqdata = param ? param : {};117 return postjson('/trainingCampLog/selectCampTemplateList', reqdata, {})118 },119 // è®ç»è¥æåºæ°æ®120 trainingCampLog_selectSortList(param) {121 let reqdata = param ? param : {};122 return postjson('/trainingCampLog/selectSortList', reqdata, {})123 },124 //æåºææè®ç»è¥åå125 trainingCampLog_updateSort (param) {126 let reqdata = param ? param : {};127 return postjson('/trainingCampLog/updateSort', reqdata, {})128 },129 // è®ç»è¥ç®¡çï¼åå¸çæ¬ï¼130 trainingCampLog_selectCampList(param) {131 let reqdata = param ? param : {};132 return postjson('/trainingCampLog/selectCampList', reqdata, {})133 },134 // è®ç»è¥è®¡åå表135 forecast_selectCampPlanList(param) {136 let reqdata = param ? param : {};137 return postjson('/forecast/selectCampPlanList', reqdata, {})138 },139 // æç»å表140 forecast_selectCoachList(param) {141 let reqdata = param ? param : {};142 return postjson('/forecast/selectCoachList', reqdata, {})143 },144 // æ´æ°è®ç»è¥è®¡å145 trainingCampLog_updateCampClassInfo(param) {146 let reqdata = param ? param : {};147 return postjson('/trainingCampLog/updatePlanCamp', reqdata, {})148 },149 // å享è¿æ¥150 trainingCampLog_selectShareUrl(param) {151 let reqdata = param ? param : {};152 return postjson('/forecast/selectShareUrl', reqdata, {})153 },154 // æéæç»155 forecast_campPlanPush(param) {156 let reqdata = param ? param : {};157 return postjson('/forecast/campPlanPush', reqdata, {})158 },159 // æ¥åå¦åå表160 forecast_selectForecastList(param) {161 let reqdata = param ? param : {};162 return postjson('/forecast/selectForecastList', reqdata, {})163 },164 // addæ¥åå¦åå表165 forecast_add(param) {166 let reqdata = param ? param : {};167 return postjson('/forecast/add', reqdata, {})168 },169 // å é¤é¢æ¥åå¦å170 forecast_delete(param) {171 let reqdata = param ? param : {};172 return postjson('/forecast/delete', reqdata, {})173 },174 // æ´æ°æ¯ä»ç¶æ175 forecast_updatePayType(param) {176 let reqdata = param ? param : {};177 return postjson('/forecast/updatePayType', reqdata, {})178 },179 // è®ç»è¥(课ç¨)æ¥è¯¢å表ï¼logï¼180 trainingCampLog_selectCampLogList(param) {181 let reqdata = param ? param : {};182 return postjson('/trainingCampLog/selectCampLogList', reqdata, {})183 },184 // å建è®ç»è¥éæ©æ¨¡æ¿å表185 trainingCampLog_selectCreateTemplateList(param) {186 let reqdata = param ? param : {};187 return postjson('/trainingCampLog/selectCreateTemplateList', reqdata, {})188 },189 // å¤å¶è®ç»è¥190 trainingCampLog_insertCopyCampLog(param) {191 let reqdata = param ? param : {};192 return postjson('/trainingCampLog/insertCopyCampLog', reqdata, {})193 },194 // å é¤è®ç»è¥log表æ°æ®195 trainingCampLog_deleteCampLog(param) {196 let reqdata = param ? param : {};197 return postjson('/trainingCampLog/deleteCampLog', reqdata, {})198 },199 // è®ç»è¥é¢è§æ¥å£200 trainingCampLog_selectCampInfo(param) {201 let reqdata = param ? param : {};202 return postjson('/trainingCampLog/selectCampInfo', reqdata, {})203 },204 // è®ç»è¥éæ©æç»æ¥å£205 trainingCampLog_selectCoachList(param) {206 let reqdata = param ? param : {};207 return postjson('/trainingCampLog/selectCoachList', reqdata, {})208 },209 // è¯ä¹¦è®ç»è¥å表210 student_selectEndCampList(param) {211 let reqdata = param ? param : {};212 return postjson('/student/selectEndCampList', reqdata, {})213 },214 // è¯ä¹¦å¦åå表215 student_selectCertificateStudentList(param) {216 let reqdata = param ? param : {};217 return postjson('/student/selectCertificateStudentList', reqdata, {})218 },219 // æ¥è¯¢å¦åå表220 student_selectCampStudentList(param) {221 let reqdata = param ? param : {};222 return postjson('/student/selectCampStudentList', reqdata, {})223 },224 //å é¤è®ç»è¥å
çå¦å225 student_deleteStudent(param) {226 let reqdata = param ? param : {};227 return postjson('/student/deleteStudent', reqdata, {})228 },229 //æ·»å å¦å230 student_addStudent(param) {231 let reqdata = param ? param : {};232 return postjson('/student/addStudent', reqdata, {})233 },234 //ç¼è¾å¦å235 student_editStudent(param) {236 let reqdata = param ? param : {};237 return postjson('/student/updateCampStudent', reqdata, {})238 },239 //å¿å¾--æ¥è¯¢240 experience_selectExperienceByCampId(param) {241 let reqdata = param ? param : {};242 return postjson('/experience/selectExperienceByCampId', reqdata, {})243 },244 //å¿å¾--å建åä¿®æ¹245 experience_insertExperienceByCampId(param) {246 let reqdata = param ? param : {};247 return postjson('/experience/insertExperienceByCampId', reqdata, {})248 },249 // æäº¤å®¡æ ¸250 trainingCampLog_updateStatus(param) {251 let reqdata = param ? param : {};252 return postjson('/trainingCampLog/commitStatus', reqdata, {})253 },254 // æ¥è¯¢è®ç»è¥å®¡æ ¸åå¸æé255 eduUser_selectToExamine(param) {256 let reqdata = param ? param : {};257 return postjson('/eduUser/selectToExamine', reqdata, {})258 },259 // è®ç»è¥å®¡æ ¸æ¥å£260 trainingCampLog_updateCampStatus(param) {261 let reqdata = param ? param : {};262 return postjson('/trainingCampLog/updateCampStatus', reqdata, {})263 },264 // åå¸æµè¯265 notAuth_testReleseCamp(param) {266 let reqdata = param ? param : {};267 return postjson('/notAuth/testReleseCamp', reqdata, {})268 },269 // åå¸270 trainingCampLog_eduReless(param) {271 let reqdata = param ? param : {};272 return postjson('/trainingCampLog/eduReless', reqdata, {})273 },274 // è·åè¯ä¹¦å表275 student_selectCertificationList(param) {276 let reqdata = param ? param : {};277 return postjson('/student/selectCertificationList', reqdata, {})278 },279 // ä¸ä¼ è¯ä¹¦280 student_addCertificate(param) {281 let reqdata = param ? param : {};282 return postjson('/student/addCertificate', reqdata, {})283 },284 // è®ç»è¥æ¥è¯¢285 search_camp(param) {286 let reqdata = param ? param : {};287 return postjson('/campComment/selectCamp', reqdata, {})288 },289 // å¦åè¯ä»·å表æ¥è¯¢290 student_comment_list(param) {291 let reqdata = param ? param : {};292 return postjson('/campComment/list', reqdata, {})293 },294 // å¦åè¯ä»·ç±»åä¿®æ¹295 student_comment_updateType(param) {296 let reqdata = param ? param : {};297 return postjson('/campComment/updateType', reqdata, {})298 },299 // å¦åè¯ä»·ç±»åæ°å¢300 student_comment_insert(param) {301 let reqdata = param ? param : {};302 return postform('/campComment/insert', reqdata, {})303 },304 // ä¼ç§ä½ä¸å表æ¥è¯¢305 student_goodJod_list(param) {306 let reqdata = param ? param : {};307 return postjson('/campHomeworkLog/list', reqdata, {})308 },309 // ä¼ç§ä½ä¸ç±»åä¿®æ¹310 student_goodJob_updateType(param) {311 let reqdata = param ? param : {};312 return postjson('/campHomeworkLog/updateType', reqdata, {})313 },314 // è®ç»è¥ä¸ä»»å¡315 camp_task_list(param) {316 let reqdata = param ? param : {};317 return postjson('/campHomeworkLog/selectTaskList', reqdata, {})318 },319 // ä»»å¡ä¸ä½ä¸320 task_homework_list(param) {321 let reqdata = param ? param : {};322 return postjson('/campHomeworkLog/selectTaskHomeworkList', reqdata, {})323 },324 // ä¼ç§ä½ä¸æ°å¢325 student_goodJob_insert(param) {326 let reqdata = param ? param : {};327 return postform('/campHomeworkLog/insert', reqdata, {})328 },329 // 精彩å享å表æ¥è¯¢330 student_share_list(param) {331 let reqdata = param ? param : {};332 return postjson('/campWonderfulShare/selectShareList', reqdata, {})333 },334 // ææ¾å表ââå
¨é¨335 student_share_video_list(param) {336 let reqdata = param ? param : {};337 return postjson('/campWonderfulShare/selectShareVideoList', reqdata, {})338 },339 // æ°å»ºæè
æ´æ°ç²¾å½©å享340 student_share_insert(param) {341 let reqdata = param ? param : {};342 return postjson('/campWonderfulShare/update', reqdata, {})343 },344 // è®ç»è¥ç»è®¡æ¥è¯¢æ¥è¯¢ è¿å
¥é¡µé¢æä¸é¢ä¸æ345 camp_statistics_check(param) {346 let reqdata = param ? param : {};347 return postjson('/campStatistic/selectCampStatistic', reqdata, {})348 },349 // è®ç»è¥å¦åç»è®¡æ¥è¯¢ è¿å
¥é¡µé¢è¡¨æ ¼350 camp_student_statistics_list(param) {351 let reqdata = param ? param : {};352 return postjson('/campStatistic/selectStudentStatisticList', reqdata, {})353 },354 // è®ç»è¥å¦åä½ä¸ç»è®¡æ¥è¯¢ ç¹å»å个å¦åæ¥è¯¢355 camp_student_statistics_info(param) {356 let reqdata = param ? param : {};357 return postjson('/campStatistic/selectStudentHomeworkStatistic', reqdata, {})358 },359 // 导åºè¡¨360 export_and_user(param) {361 let reqdata = param ? param : {};362 return postjson('/campStatistic/exportAndUser', reqdata, {})363 },364 // æ æä½ä¸å表æ¥è¯¢365 student_poleJod_list(param) {366 let reqdata = param ? param : {};367 return postjson('/benchmarking/selectList', reqdata, {})368 },369 // æ æä½ä¸ç±»åä¿®æ¹370 student_poleJob_updateType(param) {371 let reqdata = param ? param : {};372 return postjson('/benchmarking/updateType', reqdata, {})373 },374 // æ æä½ä¸æ°å¢375 student_poleJob_insert(param) {376 let reqdata = param ? param : {};377 return postform('/benchmarking/insert', reqdata, {})378 },...
posts.service.ts
Source:posts.service.ts
1import { Injectable } from '@angular/core';2import { HttpClient } from '@angular/common/http';3import { Observable, throwError, BehaviorSubject } from 'rxjs';4import { catchError } from 'rxjs/operators';5import { IPost } from '../models/IPost';6import { PostClass } from '../models/PostClass';7import { CUSTOM_FIELD_COUNTRY_ID } from '../Constants';8@Injectable({9 providedIn: 'root'10})11export class PostsService {12 url: string = 'https://api.smartrecruiters.com/v1/companies/smartrecruiters/postings';13 postsArray: IPost[] = [];14 private postsSource = new BehaviorSubject<IPost[]>([]);15 postsReference = this.postsSource.asObservable();16 constructor(private http: HttpClient) { }17 /*---HTTP REQUESTS-------------------------------------*/18 getAllPosts(): Observable<any> {19 /* OPTION1 - FROM JSON LOCAL FILE: */20 return this.getLocalJSON();21 /* // OPTION2 - FROM HTTP SERVICE:22 let data = this.http23 .get(this.url)24 .pipe(catchError(this.handleError));25 return data;26 */27 }28 getPostById(id: string): Observable<any> {29 /* OPTION1 - FROM JSON LOCAL FILE: */30 return this.getLocalJSON(id);31 /* // OPTION2 - FROM HTTP SERVICE:32 let data = this.http33 .get(`${this.url}/${id}`)34 .pipe(catchError(this.handleError));35 return data;36 */37 }38 /*---END OF HTTP REQUESTS-------------------------------------*/39 getPostsInitialization() {40 this.postsArray = []; //Reset array before adding new objects.41 this.getAllPosts().subscribe(data => {42 if (data && data.content) {43 data.content.map(postJSON => {44 if (postJSON) {45 //map to a Post object (model)46 this.postsArray.push(this.jsonToPostMapper(postJSON));47 }48 });49 // broadcast postsLists to other components50 this.postsSource.next(this.postsArray);51 }52 });53 }54 jsonToPostMapper(postJSON: any): IPost {55 let post: PostClass = new PostClass();56 if (postJSON.id) {57 post.id = postJSON.id;58 }59 if (postJSON.name) {60 post.name = postJSON.name;61 }62 if (postJSON.location && postJSON.location.city) {63 post.city = postJSON.location.city;64 }65 if (postJSON.customField66 && postJSON.customField.length > 167 && postJSON.customField[1]68 && postJSON.customField[1].fieldId69 && postJSON.customField[1].fieldId === CUSTOM_FIELD_COUNTRY_ID70 && postJSON.customField[1].valueLabel) {71 post.country = {72 code: postJSON.customField[1].valueId,73 name: postJSON.customField[1].valueLabel74 };75 }76 if (postJSON.department77 && postJSON.department.id78 && postJSON.department.label) {79 post.department = {80 id: postJSON.department.id,81 name: postJSON.department.label82 };83 }84 if (postJSON.company && postJSON.company.name) {85 post.companyName = postJSON.company.name;86 }87 //Sections:88 if (postJSON.jobAd && postJSON.jobAd.sections) {89 if (postJSON.jobAd.sections.companyDescription90 && postJSON.jobAd.sections.companyDescription.text) {91 post.companyDescription = postJSON.jobAd.sections.companyDescription.text;92 }93 if (postJSON.jobAd.sections.jobDescription94 && postJSON.jobAd.sections.jobDescription.text) {95 post.jobDescription = postJSON.jobAd.sections.jobDescription.text;96 }97 if (postJSON.jobAd.sections.qualifications98 && postJSON.jobAd.sections.qualifications.text) {99 post.qualifications = postJSON.jobAd.sections.qualifications.text;100 }101 if (postJSON.jobAd.sections.additionalInformation102 && postJSON.jobAd.sections.additionalInformation.text) {103 post.additionalInfo = postJSON.jobAd.sections.additionalInformation.text;104 }105 }106 return post;107 }108 handleError(error) {109 let errorMessage = '';110 if (error.error instanceof ErrorEvent) {111 // client-side error112 errorMessage = `Error: ${error.error.message}`;113 } else {114 // server-side error115 errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`;116 }117 return throwError(errorMessage);118 }119 getLocalJSON(id?: string): Observable<any> {120 return id ? this.http.get(`assets/${id}.json`) : this.http.get("assets/data.json");121 }...
Using AI Code Generation
1const http = require("http");2const options = {3 headers: {4 },5};6const req = http.request(options, (res) => {7 console.log(`statusCode: ${res.statusCode}`);8 res.on("data", (d) => {9 process.stdout.write(d);10 });11});12req.on("error", (error) => {13 console.error(error);14});15req.write(16 JSON.stringify({17 {18 {19 equals: {20 },21 },22 {23 is: {24 headers: {25 },26 body: JSON.stringify({27 }),28 },29 },30 },31 })32);33req.end();34{"message":"Hello World!"}
Using AI Code Generation
1var request = require('request');2var options = {3 json: {4 {5 {6 "is": {7 "headers": {8 },9 "body": {10 }11 }12 }13 }14 }15};16request(options, function (error, response, body) {17 console.log(body);18});19var request = require('request');20var options = {21 json: {22 {23 {24 "is": {25 "headers": {26 },27 "body": {28 }29 }30 }31 },32 {33 {34 "is": {35 "headers": {36 },37 "body": {38 }39 }40 }41 }42 }43};44request(options, function (error, response, body) {45 console.log(body);46});47var request = require('request');48var options = {49};50request(options, function (error, response, body) {51 console.log(body);52});
Using AI Code Generation
1var request = require('request');2var options = {3 json: {4 {5 {6 "is": {7 "headers": {8 },9 "body": {10 }11 }12 }13 }14 }15};16request(options, function (error, response, body) {17 if (!error && response.statusCode == 201) {18 console.log(body);19 }20});21var request = require('request');22var options = {23 json: {24 {25 {26 "is": {27 "headers": {28 },29 "body": {30 }31 }32 }33 }34 }35};36request(options, function (error, response, body) {37 if (!error && response.statusCode == 201) {38 console.log(body);39 }40});41var request = require('request');42var options = {43 json: {44 {45 {46 "is": {47 "headers": {48 },49 "body": {50 }51 }52 }
Using AI Code Generation
1var request = require('request');2var options = {3 json: {4 {5 {6 "is": {7 "headers": {8 },9 "body": {10 }11 }12 }13 }14 }15};16function callback(error, response, body) {17 if (!error && response.statusCode == 201) {18 console.log(body);19 }20}21request(options, callback);22var request = require('request');23var options = {24 json: {25 {26 {27 "is": {28 "headers": {29 },30 "body": {31 }32 }33 }34 }35 }36};37function callback(error, response, body) {38 if (!error && response.statusCode == 201) {39 console.log(body);40 }41}42request(options, callback);43var request = require('request');44var options = {45 json: {46 {47 {48 "is": {49 "headers": {50 },51 "body": {52 }53 }54 }55 }
Using AI Code Generation
1const rp = require('request-promise');2const options = {3 body: {4 stubs: [{5 predicates: [{6 equals: {7 }8 }],9 responses: [{10 is: {11 headers: {12 },13 body: JSON.stringify({14 })15 }16 }]17 }]18 },19};20rp(options)21 .then(function (parsedBody) {22 console.log(parsedBody);23 })24 .catch(function (err) {25 console.log(err);26 });27{ port: 3000, protocol: 'http', numberOfRequests: 0, stubs: [ { predicates: [ [Object] ], responses: [ [Object] ] } ], _links: { self: { href: '/imposters/3000' } } }28{29}30{ port: 3000, protocol: 'http', numberOfRequests: 0, stubs: [ { predicates: [ [Object] ], responses: [ [Object] ] } ], _links: { self: { href: '/imposters/3000' } } }31{ errors: [ 'Cannot DELETE /imposters/3000' ] }
Using AI Code Generation
1var mb = require('mountebank');2 {3 {4 {5 is: {6 headers: {7 },8 body: JSON.stringify({ success: true })9 }10 }11 }12 }13];14mb.postJSON('/imposters', imposters, function (error, response) {15 if (error) {16 console.error('Error creating imposter: ', error);17 }18 else {19 console.log('Created imposter');20 }21});22var mb = require('mountebank');23 {24 {25 {26 is: {27 headers: {28 },29 body: JSON.stringify({ success: true })30 }31 }32 }33 }34];35mb.post('/imposters', imposters, function (error, response) {36 if (error) {37 console.error('Error creating imposter: ', error);38 }39 else {40 console.log('Created imposter');41 }42});43var mb = require('mountebank');44mb.getJSON('/imposters', function (error, response) {45 if (error) {46 console.error('Error getting imposters: ', error);47 }48 else {49 console.log('Got imposters: ', response.body);50 }51});52var mb = require('mountebank');53mb.get('/imposters', function (error, response) {54 if (error) {55 console.error('Error getting imposters: ', error);56 }57 else {58 console.log('Got imposters: ', response.body);59 }60});61var mb = require('mountebank');62mb.deleteJSON('/imposters/3000', function (
Using AI Code Generation
1var mb = require('mountebank');2var request = require('request');3var mbHelper = mb.create({ port: 2525, pidfile: 'mb.pid', logfile: 'mb.log' });4mbHelper.postJSON('/imposters', {5 {6 {7 is: {8 }9 }10 }11}, function (error, response) {12 if (error) {13 console.error('Failed to create imposter: ' + error.message);14 } else {15 console.log(body);16 mbHelper.del('/imposters/' + response.port, function (error) {17 if (error) {18 console.error('Failed to delete imposter: ' + error.message);19 } else {20 console.log('Deleted imposter');21 }22 });23 });24 }25});26{27 "dependencies": {28 }29}
Using AI Code Generation
1var postJSON = require('mbjs').postJSON;2var options = {3 headers: {4 }5};6 {7 {8 {9 is: {10 headers: {11 },12 body: JSON.stringify({foo: 'bar'})13 }14 }15 }16 }17];18postJSON(options, imposters, function (error, response) {19 if (error) {20 console.error('Error posting JSON:', error);21 } else {22 console.log('Response:', response);23 }24});25var postJSON = require('mbjs').postJSON;26var options = {27 headers: {28 }29};30 {31 {32 {33 is: {34 headers: {35 },36 body: JSON.stringify({foo: 'bar'})37 }38 }39 }40 }41];42postJSON(options, imposters, function (error, response) {43 if (error) {44 console.error('Error posting JSON:', error);45 } else {46 console.log('Response:', response);47 }48});49var postJSON = require('mbjs').postJSON;50var options = {51 headers: {52 }53};54 {
Using AI Code Generation
1var postJSON = function(url, obj) {2 var request = require('request');3 request.post({4 }, function(error, response, body) {5 console.log(body);6 });7};8var getJSON = function(url) {9 var request = require('request');10 request.get({11 }, function(error, response, body) {12 console.log(body);13 });14};15var deleteJSON = function(url) {16 var request = require('request');17 request.del({18 }, function(error, response, body) {19 console.log(body);20 });21};22var putJSON = function(url, obj) {23 var request = require('request');24 request.put({25 }, function(error, response, body) {26 console.log(body);27 });28};29var patchJSON = function(url, obj) {30 var request = require('request');31 request.patch({32 }, function(error, response, body) {33 console.log(body);34 });35};36var headJSON = function(url) {37 var request = require('request');38 request.head({39 }, function(error, response, body) {40 console.log(body);41 });42};43var optionsJSON = function(url) {44 var request = require('request');45 request.options({46 }, function(error, response, body) {47 console.log(body);48 });49};50var deleteJSON = function(url) {51 var request = require('request');52 request.del({53 }, function(error, response, body) {54 console.log(body);55 });56};57var deleteJSON = function(url) {58 var request = require('
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!