11import { jest } from "@jest/globals" ; // eslint-disable-line import/no-extraneous-dependencies
22import organizationModel from "#models/organization" ;
33import connector from "#models/databaseUtil" ;
4+ import accreditationModel from "#models/accreditation" ;
45
56jest . mock ( "#util" ) ;
67const { agent } = global ;
8+ let accreditationIds ;
9+ let parentId ;
710
811function cleanUp ( callback ) {
912 organizationModel . remove ( { startDate : "2023-06-18T14:11:30Z" } ) . then ( ( ) => {
@@ -15,60 +18,72 @@ function cleanUp(callback) {
1518 } ) ;
1619 } ) ;
1720}
21+ /* eslint-disable no-underscore-dangle */
22+ async function getIds ( callback ) {
23+ accreditationIds = await accreditationModel . read ( { } , 1 ) ;
24+ accreditationIds = accreditationIds . data [ 0 ] . _id ;
25+ parentId = await organizationModel . read ( { } , 1 ) ;
26+ parentId = parentId . data [ 0 ] . _id ;
27+ callback ( ) ;
28+ }
29+
30+ beforeAll ( ( done ) => {
31+ getIds ( done ) ;
32+ } ) ;
1833
1934afterAll ( ( done ) => {
2035 cleanUp ( done ) ;
2136} ) ;
2237
2338describe ( "Organization API" , ( ) => {
24- it ( "should create organization" , async ( ) => {
25- const response = await agent . post ( "/organization/add" ) . send ( {
26- parent : "60a0e7e9a09c3f001c834e06" ,
27- startDate :"2023-06-18T14:11:30Z" ,
28- name :"my org" ,
29- accreditations : "60a0e7e9a09c3f001c834e06" ,
30- } ) ;
31-
32- expect ( response . status ) . toBe ( 200 ) ;
33- expect ( response . body . res ) . toMatch ( / a d d e d o r g a n i z a t i o n / ) ;
39+ it ( "should create organization" , async ( ) => {
40+ const response = await agent . post ( "/organization/add" ) . send ( {
41+ parent : parentId ,
42+ startDate : "2023-06-18T14:11:30Z" ,
43+ name : "my org" ,
44+ accreditations : accreditationIds ,
3445 } ) ;
35-
36- describe ( "after adding organization" , ( ) => {
37- let id ;
38- beforeEach ( async ( ) => {
39- id = await agent . post ( "/organization/add" ) . send ( {
40- parent : "60a0e7e9a09c3f001c834e06" ,
41- startDate :"2023-06-18T14:11:30Z" ,
42- name :"my org" ,
43- accreditations : "60a0e7e9a09c3f001c834e06" ,
44- } ) ;
45- id = JSON . parse ( id . res . text ) . id ;
46- } ) ;
47-
48- afterEach ( async ( ) => {
49- await organizationModel . remove ( {
50- parent : "60a0e7e9a09c3f001c834e06" ,
51- startDate :"2023-06-18T14:11:30Z" ,
52- name :"my org" ,
53- accreditations : "60a0e7e9a09c3f001c834e06" ,
54- } ) ;
55- } ) ;
56-
57- it ( "should read organization" , async ( ) => {
58- const response = await agent
59- . get ( "/organization/list" )
60- . send ( { name :"my org" } ) ;
61- expect ( response . status ) . toBe ( 200 ) ;
62- expect ( response . body . res ) . toBeDefined ( ) ;
46+
47+ expect ( response . status ) . toBe ( 200 ) ;
48+ expect ( response . body . res ) . toMatch ( / a d d e d o r g a n i z a t i o n / ) ;
49+ } ) ;
50+
51+ describe ( "after adding organization" , ( ) => {
52+ let id ;
53+ beforeEach ( async ( ) => {
54+ id = await agent . post ( "/organization/add" ) . send ( {
55+ parent : parentId ,
56+ startDate : "2023-06-18T14:11:30Z" ,
57+ name : "my org" ,
58+ accreditations : accreditationIds ,
6359 } ) ;
64-
65- it ( "should update organization" , async ( ) => {
66- const response = await agent
67- . post ( `/organization/update/${ id } ` )
68- . send ( { name : "your org" } ) ;
69-
70- expect ( response . status ) . toBe ( 200 ) ;
71- expect ( response . body . res ) . toMatch ( / o r g a n i z a t i o n u p d a t e d / ) ;
60+ id = JSON . parse ( id . res . text ) . id ;
61+ } ) ;
62+
63+ afterEach ( async ( ) => {
64+ await organizationModel . remove ( {
65+ parent : parentId ,
66+ startDate : "2023-06-18T14:11:30Z" ,
67+ name : "my org" ,
68+ accreditations : accreditationIds ,
7269 } ) ;
7370 } ) ;
74- } ) ;
71+
72+ it ( "should read organization" , async ( ) => {
73+ const response = await agent
74+ . get ( "/organization/list" )
75+ . send ( { name : "my org" } ) ;
76+ expect ( response . status ) . toBe ( 200 ) ;
77+ expect ( response . body . res ) . toBeDefined ( ) ;
78+ } ) ;
79+
80+ it ( "should update organization" , async ( ) => {
81+ const response = await agent
82+ . post ( `/organization/update/${ id } ` )
83+ . send ( { name : "your org" } ) ;
84+
85+ expect ( response . status ) . toBe ( 200 ) ;
86+ expect ( response . body . res ) . toMatch ( / o r g a n i z a t i o n u p d a t e d / ) ;
87+ } ) ;
88+ } ) ;
89+ } ) ;
0 commit comments