@@ -446,6 +446,195 @@ describe('Model methods', () => {
446
446
})
447
447
})
448
448
449
+
test('save() method makes a POST request when ID of object does not exists, with header "Content-Type: multipart/form-data" if the data has files', async () => {
450
+
let post
451
+
const file = new File(["foo"], "foo.txt", {
452
+
type: "text/plain",
453
+
})
454
+
const _postResponse = {
455
+
id: 1,
456
+
title: 'Cool!'
457
+
}
458
+
459
+
axiosMock.onAny().reply((config) => {
460
+
let _data
461
+
462
+
if (config.headers['Content-Type'] === 'multipart/form-data') {
463
+
_data = Object.fromEntries(config.data)
464
+
465
+
if (_data['files[]']) {
466
+
_data.files = [{}, {}]
467
+
delete _data['files[]']
468
+
}
469
+
470
+
_data = JSON.stringify(_data)
471
+
} else {
472
+
_data = config.data
473
+
}
474
+
475
+
expect(config.method).toEqual('post')
476
+
expect(config.headers['Content-Type']).toEqual('multipart/form-data')
477
+
expect(_data).toEqual(JSON.stringify(post))
478
+
expect(config.url).toEqual('http://localhost/posts')
479
+
480
+
return [200, _postResponse]
481
+
})
482
+
483
+
// Single files
484
+
post = new Post({ title: 'Cool!', file })
485
+
await post.save()
486
+
487
+
// Multiple files
488
+
post = new Post({ title: 'Cool!', files: [file, file] })
489
+
await post.save()
490
+
})
491
+
492
+
test('save() method makes a PUT request when ID of when ID of object exists, with header "Content-Type: multipart/form-data" if the data has files', async () => {
493
+
let post
494
+
const file = new File(["foo"], "foo.txt", {
495
+
type: "text/plain",
496
+
})
497
+
const _postResponse = {
498
+
id: 1,
499
+
title: 'Cool!'
500
+
}
501
+
502
+
axiosMock.onAny().reply((config) => {
503
+
let _data
504
+
505
+
if (config.headers['Content-Type'] === 'multipart/form-data') {
506
+
_data = Object.fromEntries(config.data)
507
+
_data.id = 1
508
+
509
+
if (_data['files[]']) {
510
+
_data.files = [{}, {}]
511
+
delete _data['files[]']
512
+
}
513
+
514
+
_data = JSON.stringify(_data)
515
+
} else {
516
+
_data = config.data
517
+
}
518
+
519
+
expect(config.method).toEqual('put')
520
+
expect(config.headers['Content-Type']).toEqual('multipart/form-data')
521
+
expect(_data).toEqual(JSON.stringify(post))
522
+
expect(config.url).toEqual('http://localhost/posts/1')
523
+
524
+
return [200, _postResponse]
525
+
})
526
+
527
+
// Single file
528
+
post = new Post({ id: 1, title: 'Cool!', file })
529
+
await post.save()
530
+
531
+
// Multiple files
532
+
post = new Post({ id: 1, title: 'Cool!', files: [file, file] })
533
+
await post.save()
534
+
})
535
+
536
+
test('patch() method makes a PATCH request when ID of when ID of object exists, with header "Content-Type: multipart/form-data" if the data has files', async () => {
537
+
let post
538
+
const file = new File(["foo"], "foo.txt", {
539
+
type: "text/plain",
540
+
})
541
+
const _postResponse = {
542
+
id: 1,
543
+
title: 'Cool!'
544
+
}
545
+
546
+
axiosMock.onAny().reply((config) => {
547
+
let _data
548
+
const _post = post
549
+
delete _post._config
550
+
551
+
if (config.headers['Content-Type'] === 'multipart/form-data') {
552
+
_data = Object.fromEntries(config.data)
553
+
_data.id = 1
554
+
555
+
if (_data['files[]']) {
556
+
_data.files = [{}, {}]
557
+
delete _data['files[]']
558
+
}
559
+
560
+
_data = JSON.stringify(_data)
561
+
} else {
562
+
_data = config.data
563
+
}
564
+
565
+
expect(config.method).toEqual('patch')
566
+
expect(config.headers['Content-Type']).toEqual('multipart/form-data')
567
+
expect(_data).toEqual(JSON.stringify(_post))
568
+
expect(config.url).toEqual('http://localhost/posts/1')
569
+
570
+
return [200, _postResponse]
571
+
})
572
+
573
+
// Single file
574
+
post = new Post({ id: 1, title: 'Cool!', file })
575
+
await post.patch()
576
+
577
+
// Multiple files
578
+
post = new Post({ id: 1, title: 'Cool!', files: [file, file] })
579
+
await post.patch()
580
+
})
581
+
582
+
test('save() method can add header "Content-Type: multipart/form-data" when "headers" object is already defined', async () => {
583
+
let post
584
+
const file = new File(["foo"], "foo.txt", {
585
+
type: "text/plain",
586
+
})
587
+
const _postResponse = {
588
+
id: 1,
589
+
title: 'Cool!',
590
+
text: 'Lorem Ipsum Dolor',
591
+
user: {
592
+
firstname: 'John',
593
+
lastname: 'Doe',
594
+
age: 25
595
+
},
596
+
relationships: {
597
+
tags: [
598
+
{
599
+
name: 'super'
600
+
},
601
+
{
602
+
name: 'awesome'
603
+
}
604
+
]
605
+
}
606
+
}
607
+
608
+
axiosMock.onAny().reply((config) => {
609
+
let _data
610
+
const _post = post
611
+
delete _post._config
612
+
613
+
if (config.headers['Content-Type'] === 'multipart/form-data') {
614
+
_data = JSON.stringify(Object.fromEntries(config.data))
615
+
} else {
616
+
_data = config.data
617
+
}
618
+
619
+
expect(config.method).toEqual('post')
620
+
expect(config.headers['Content-Type']).toStrictEqual('multipart/form-data')
621
+
expect(_data).toEqual(JSON.stringify(_post))
622
+
expect(config.url).toEqual('http://localhost/posts')
623
+
624
+
return [200, _postResponse]
625
+
})
626
+
627
+
post = new Post({ title: 'Cool!', file })
628
+
post = await post.config({ headers: {} }).save()
629
+
630
+
expect(post).toEqual(_postResponse)
631
+
expect(post).toBeInstanceOf(Post)
632
+
expect(post.user).toBeInstanceOf(User)
633
+
post.relationships.tags.forEach(tag => {
634
+
expect(tag).toBeInstanceOf(Tag)
635
+
})
636
+
})
637
+
449
638
test('save() method makes a POST request when ID of object is null', async () => {
450
639
let post
451
640
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4