Unit Testing: CBT Context Report - CBTYoung/Documentation GitHub Wiki

import AsyncStorage from "@react-native-async-storage/async-storage";
import { Value } from "react-native-reanimated";
import { CBTReportContext } from "../../../src/architecture/business_layer/modules/CBTReportContext";
import { ActiveFill } from "../../../src/architecture/business_layer/modules/ReportStrategies/ActiveFill";
import { BodyPartSelection, bodyParts } from "../../../src/architecture/business_layer/modules/ReportStrategies/BodyPartSelection";
import { DType, getValue, wipe_guest, removeValueofReport, saveValueofReport } from "../../../src/architecture/data_access_layer/local_storage";


    describe("Active Fill Summary tests", () => {

        beforeEach(()=>{
            wipe_guest();
        });

        test('checking getsummary with one report', () =>{
            const date = new Date(Date.now());
            const fill = new ActiveFill("feeling good",[],new BodyPartSelection(bodyParts.stomach), "my", "world");
            const report = new CBTReportContext(date,fill);
            expect(date.toString()+":\n"+fill.getSummary())
            .toBe(report.getSummary());
        });
    
        test('checking get summary with two reports', () =>{
            const date = new Date(Date.now());
            const fill = new ActiveFill("feeling good");
            const report = new CBTReportContext(date,fill);
            expect(date.toString()+":\n"+fill.getSummary())
            .toBe(report.getSummary());
        });
    
        
        test('checking get summary with only event  and emotion data', () =>{
            const date = new Date(Date.now());
            const fill = new ActiveFill("feeling good", [],new BodyPartSelection(bodyParts.stomach));
            const report = new CBTReportContext(date,fill);
            expect(date.toString()+":\n"+fill.getSummary())
            .toBe(report.getSummary());
        });
    
        
        test('checking get summary with only event and body part data', () =>{
            const date = new Date(Date.now());
            const fill = new ActiveFill("feeling good", [],new BodyPartSelection(bodyParts["leg"]), "my", "world");
            const report = new CBTReportContext(date,fill);
            expect(date.toString()+":\n"+fill.getSummary())
            .toBe(report.getSummary());
        });

     

        
        test('checking async storage', async () =>{
            const date = new Date(Date.now());
            const fill = new ActiveFill("feeling good", [],new BodyPartSelection(bodyParts["leg"]), "my", "world");
            const report = new CBTReportContext(date,fill);
            expect(date.toString()+":\n"+fill.getSummary())
            .toBe(report.getSummary());

            await saveValueofReport("Guy", DType.CBTReportContext, report);
            let storedreport = await getValue({"UserName": "Guy","index":"0", "DataType":DType.CBTReportContext})
            if(storedreport!= undefined)
            {
                expect(storedreport.constructor.name).toBe(CBTReportContext.name);
                expect(storedreport.getSummary())
             .toBe(report.getSummary());}
             
        });


          
        test('checking async storage', async () =>{
            const date = new Date(Date.now());
            const fill = new ActiveFill("feeling good", [],new BodyPartSelection(bodyParts.stomach), "my", "world");
            const report = new CBTReportContext(date,fill);
            expect(date.toString()+":\n"+fill.getSummary())
            .toBe(report.getSummary());

            await saveValueofReport("Guy", DType.CBTReportContext, report)
            let storedreport = await getValue({"UserName": "Guy","index":"0", "DataType":DType.CBTReportContext})
            if(storedreport!= undefined)
            {
                expect(storedreport.constructor.name).toBe(CBTReportContext.name);
                expect(storedreport.getSummary())
             .toBe(report.getSummary());
            }
             
        });

        
        test('checking async storage two items', async () =>{
            const date = new Date(Date.now());
            const fill1 = new ActiveFill("feeling good", [],new BodyPartSelection(bodyParts.stomach), "my", "world");
            const report1 = new CBTReportContext(date,fill1);
            expect(date.toString()+":\n"+fill1.getSummary())
            .toBe(report1.getSummary());

            const fill2 = new ActiveFill("feeling even better");
            const report2 = new CBTReportContext(date,fill2);
            expect(date.toString()+":\n"+fill2.getSummary())
            .toBe(report2.getSummary());

            await saveValueofReport("Guy", DType.CBTReportContext, report1)
            await saveValueofReport("Guy", DType.CBTReportContext, report2)

            let storedreport2 = await getValue({"UserName": "Guy","index":"1", "DataType":DType.CBTReportContext})
            if(storedreport2!= undefined)
            {
                expect(storedreport2.constructor.name).toBe(CBTReportContext.name);
                expect(storedreport2.getSummary())
             .toBe(report2.getSummary());}
             let storedreport1 = await getValue({"UserName": "Guy","index":"0", "DataType":DType.CBTReportContext})
             if(storedreport1!= undefined)
             {
                 expect(storedreport1.constructor.name).toBe(CBTReportContext.name);
                 expect(storedreport1.getSummary())
              .toBe(report1.getSummary());}
              
        });

           
        test('checking async not work', async () =>{
            const date = new Date(Date.now());
            const fill = new ActiveFill("feeling good", [],new BodyPartSelection(bodyParts.stomach), "my", "world");
            const report = new CBTReportContext(date,fill);
            expect(date.toString()+":\n"+fill.getSummary())
            .toBe(report.getSummary());

            await saveValueofReport("Guy", DType.CBTReportContext, report);
            await expect(async ()=>  getValue({"UserName": "Guy","index":"2", "DataType":DType.CBTReportContext})).rejects.toThrow("Problem in Storage");
        
        });


        test('checking async not work then do work', async () =>{
            const date = new Date(Date.now());
            const fill = new ActiveFill("feeling good", [],new BodyPartSelection(bodyParts.stomach), "my", "world");
            const report = new CBTReportContext(date,fill);
            expect(date.toString()+":\n"+fill.getSummary())
            .toBe(report.getSummary());

            await saveValueofReport("Guy", DType.CBTReportContext, report);
            await expect(async ()=>  getValue({"UserName": "Guy","index":"2", "DataType":DType.CBTReportContext})).rejects.toThrow("Problem in Storage");
            let storedreport = await getValue({"UserName": "Guy","index":"0", "DataType":DType.CBTReportContext})
            if(storedreport!= undefined)
            {
                expect(storedreport.constructor.name).toBe(CBTReportContext.name);
                expect(storedreport.getSummary())
             .toBe(report.getSummary());
            }
        });


        
        test('checking async add then remove', async () =>{
            const date = new Date(Date.now());
            const fill = new ActiveFill("feeling good", [],new BodyPartSelection(bodyParts.stomach), "my", "world");
            const report = new CBTReportContext(date,fill);
            expect(date.toString()+":\n"+fill.getSummary())
            .toBe(report.getSummary());

            await saveValueofReport("Guy", DType.CBTReportContext, report);
            let storedreport = await getValue({"UserName": "Guy","index":"0", "DataType":DType.CBTReportContext})
            if(storedreport!= undefined)
            {
                expect(storedreport.constructor.name).toBe(CBTReportContext.name);
                expect(storedreport.getSummary())
             .toBe(report.getSummary());
            }
            await removeValueofReport("Guy","0", DType.CBTReportContext)
            await expect(async ()=>  getValue({"UserName": "Guy","index":"0", "DataType":DType.CBTReportContext})).rejects.toThrow("Problem in Storage");
            
        });

        test('checking async add then remove', async () =>{
            const date = new Date(Date.now());
            const fill1 = new ActiveFill("feeling good", [],new BodyPartSelection(bodyParts.stomach), "my", "world");
            const report1 = new CBTReportContext(date,fill1);
            expect(date.toString()+":\n"+fill1.getSummary())
            .toBe(report1.getSummary());

            const fill2 = new ActiveFill("feeling even better");
            const report2 = new CBTReportContext(date,fill2,1,date);
            expect(date.toString()+":\n"+fill2.getSummary())
            .toBe(report2.getSummary());

            await saveValueofReport("Guy", DType.CBTReportContext, report1)
            await saveValueofReport("Guy", DType.CBTReportContext, report2)

            await removeValueofReport("Guy","0", DType.CBTReportContext)
            await expect(async ()=>  getValue({"UserName": "Guy","index":"0", "DataType":DType.CBTReportContext})).rejects.toThrow("Problem in Storage");

             let storedreport2 = await getValue({"UserName": "Guy","index":"1", "DataType":DType.CBTReportContext})
            if(storedreport2!= undefined)
            {
                expect(storedreport2.constructor.name).toBe(CBTReportContext.name);
                expect(storedreport2.getSummary())
             .toBe(report2.getSummary());}
        });
    });