1- const request = require ( 'supertest' ) ;
2- const express = require ( 'express' ) ;
3- const session = require ( 'express-session' ) ;
4- const MemoryStore = require ( 'memorystore' ) ( session ) ;
5- const commtrackr = require ( '../index' ) ;
1+ const commtrackr = require ( '../index.js' ) ;
62
7- describe ( 'Tenant customText' , ( ) => {
8- test ( 'customText' , async ( ) => {
9- commtrackr . init ( {
10- tenant : { name : 'TestTenant' , domain : 'http://localhost:3000' , path : '' , customText : { adminTitle : 'Custom Admin Title' } } ,
11- vars : { userId : 'id' , userName : 'name' , commissions : 'commissions' } ,
12- fields : [ { id : 'name' , label : 'Name' , type : 'text' } ] ,
3+ async function createHandler ( data ) {
4+ return data ;
5+ } ;
6+
7+ async function updateHandler ( req , data ) {
8+ return data ;
9+ } ;
10+
11+ async function syncHandler ( req , data ) {
12+ return data ;
13+ } ;
14+ describe ( 'CommTrackr Backend Initialization' , ( ) => {
15+ test ( 'on' , ( ) => {
16+ expect ( typeof commtrackr . on ) . toBe ( 'function' ) ;
17+ expect ( ( ) => commtrackr . on ( ) ) . not . toThrow ( ) ;
18+ } ) ;
19+ test ( 'init' , ( ) => {
20+ commtrackr . on ( ) ;
21+ expect ( typeof commtrackr . init ) . toBe ( 'function' ) ;
22+ expect ( ( ) => commtrackr . init ( {
23+ tenant : {
24+ slug : 'commtrackr' ,
25+ name : 'CommTrackr' ,
26+ description : 'Testing' ,
27+ logo : 'http://localhost:3000/logo.png' ,
28+ domain : 'http://localhost:3000' ,
29+ path : '/commissions' ,
30+ auth : {
31+ enabled : true ,
32+ provider : 'Test Authentication Provider' ,
33+ url : '/login'
34+ } ,
35+ } ,
36+ vars : {
37+ userId : 'id' ,
38+ name : 'name' ,
39+ access : {
40+ var : 'access' ,
41+ user : [ 0 ] ,
42+ dev : [ 1 ] ,
43+ admin : [ 2 ]
44+ } ,
45+ } ,
46+ fields : [ 'text' , 'number' , 'date' , 'textarea' , 'checkbox' , 'radio' , 'select' , 'multiselect' ] . flatMap ( fieldType => [
47+ {
48+ id : `test-${ fieldType } ` ,
49+ type : fieldType ,
50+ label : fieldType ,
51+ description : 'description' ,
52+ placeholder : 'placeholder' ,
53+ required : false ,
54+ options : ( ( fieldType === 'radio' ) || fieldType . includes ( 'select' ) ) ? [
55+ { label : 'Option 1' , value : 'option1' } ,
56+ { label : 'Option 2' , value : 'option2' } ,
57+ { label : 'Option 3' , value : 'option3' }
58+ ] : [ ]
59+ } ,
60+ {
61+ id : `test-${ fieldType } -required` ,
62+ type : fieldType ,
63+ label : fieldType ,
64+ description : 'description' ,
65+ placeholder : 'placeholder' ,
66+ required : true ,
67+ options : ( ( fieldType === 'radio' ) || fieldType . includes ( 'select' ) ) ? [
68+ { label : 'Option 1' , value : 'option1' } ,
69+ { label : 'Option 2' , value : 'option2' } ,
70+ { label : 'Option 3' , value : 'option3' }
71+ ] : [ ]
72+ }
73+ ] ) ,
1374 handlers : {
14- create : async ( ) => { } ,
15- update : async ( ) => { } ,
16- sync : async ( ) => { }
75+ create : createHandler ,
76+ update : updateHandler ,
77+ sync : syncHandler
1778 }
18- } ) ;
19- const app = express ( ) ;
20- app . use ( express . urlencoded ( { extended : true } ) ) ;
21- app . use ( express . json ( ) ) ;
22- app . use ( session ( { secret : 'test' , resave : false , saveUninitialized : true , store : new MemoryStore ( { checkPeriod : 86400000 } ) } ) ) ;
23- app . use ( '/' , commtrackr . routes ) ;
24- commtrackr . on ( true ) ;
25- const agent = request . agent ( app ) ;
26- await agent . get ( '/' ) ;
27- app . get ( '/set-admin' , ( req , res ) => {
28- req . session . id = 'admin1' ;
29- req . session . role = 'admin' ;
30- req . session . name = 'Admin One' ;
31- req . session . commissions = [ ] ;
32- res . json ( { ok : true } ) ;
33- } ) ;
34- await agent . get ( '/set-admin' ) ;
35- const res = await agent . get ( '/' ) ;
36- expect ( res . status ) . toBe ( 200 ) ;
37- expect ( res . text . length ) . toBeGreaterThan ( 0 ) ;
38- } , 10000 ) ;
39- } ) ;
79+ } ) ) . not . toThrow ( ) ;
80+ } ) ;
81+ test ( 'handlers' , async ( ) => {
82+ expect ( typeof createHandler ) . toBe ( 'function' ) ;
83+ var result = await createHandler ( { test : 'data' } ) ;
84+ expect ( typeof result ) . toBe ( 'object' ) ;
85+ expect ( result ) . toHaveProperty ( 'test' , 'data' ) ;
86+ expect ( typeof updateHandler ) . toBe ( 'function' ) ;
87+ result = await updateHandler ( { } , { test : 'data' } ) ;
88+ expect ( typeof result ) . toBe ( 'object' ) ;
89+ expect ( result ) . toHaveProperty ( 'test' , 'data' ) ;
90+ expect ( typeof syncHandler ) . toBe ( 'function' ) ;
91+ result = await syncHandler ( { } , { test : 'data' } ) ;
92+ expect ( typeof result ) . toBe ( 'object' ) ;
93+ expect ( result ) . toHaveProperty ( 'test' , 'data' ) ;
94+ } ) ;
95+ } ) ;
0 commit comments