Unit Test Native Storage


#1

Hi guys!

I’m doing Unit testing and one of my Provider uses NativeStorage like this:

@Injectable()
export class LocalStorageProvider {


    constructor(private nativeStorage: NativeStorage) {
        console.log('Hello LocalStorageProvider Provider');
    }


    /**
     * Set the UserLoggedIn Object in Local Storage
     * @returns {Promise<any>}
     */
    setUserLoggedIn(): Promise<any>{

        return this.nativeStorage.setItem(Constants.USER_LOGGED_IN,{isLoggedIn:true});

    }

    /**
     * Remove the UserLoggedIn Object from Local Storage
     * @returns {Promise<any>}
     */
    removeUserLoggedIn():Promise<any>{

        return this.nativeStorage.remove(Constants.USER_LOGGED_IN);
    }


    removeUserFromStorage(): Promise<any> {
        return this.nativeStorage.remove(Constants.LOCAL_STORAGE_USER);
    }


    /**
     * Save the Collected User Data to Local Storage
     * @param {this} context
     * @param user
     */
    saveUserDataToLocalStorage(user:User): Promise<any> {

        return this.nativeStorage.setItem(Constants.LOCAL_STORAGE_USER,
            user);
    }


    /**
     * Get the User Object from Local Storage
     * @returns {Promise<User>}
     */
    getUserFromLocalStorage(): Promise<User>{

        return this.nativeStorage.getItem(Constants.LOCAL_STORAGE_USER);
    }

}

My issue is that I have no idea how to test it properly…
My Test so far:

describe('LocalStorageProvider', () => {

    let provider: LocalStorageProvider;

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [],

            providers: [
                LocalStorageProvider,
                // NativeStorage
            ],

            imports: [
                AppModule
            ]
        }).compileComponents();

        provider = TestBed.get(LocalStorageProvider);


    }));


    afterEach(() => {
        provider = null;
    });


    // TEST that the Provider is created properly
    it('should be created', () => {
        expect(provider).toBeTruthy();
    });




});

Could you help me please?

Thanks!