Skip to content

Commit

Permalink
added localstorage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Egge21M committed Nov 12, 2023
1 parent dbfa223 commit 42fc24b
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
77 changes: 77 additions & 0 deletions src/localstorage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { describe, test, expect, afterEach } from '@jest/globals';
import { signFromLocalstorage } from './messages';

Check failure on line 2 in src/localstorage.test.ts

View workflow job for this annotation

GitHub Actions / build

Module '"./messages"' has no exported member 'signFromLocalstorage'.
import { EventTemplate, generatePrivateKey } from 'nostr-tools';
import { getPublicKeyFromLocalstorage } from './localstorage';

class LocalStorageMock {
store: {
[key: string]: string;
};
constructor() {
this.store = {};
}

clear() {
this.store = {};
}

getItem(key: string) {
return this.store[key] || null;
}

setItem(key: string, value: string) {
this.store[key] = String(value);
}

removeItem(key: string) {
delete this.store[key];
}
}

//@ts-ignore
global.localStorage = new LocalStorageMock();

describe('signing a message from localstorage', () => {
afterEach(() => {
localStorage.removeItem('pleb_sk');
});
test('assume private key is found', () => {
localStorage.setItem('pleb_sk', generatePrivateKey());
const unsignedEvent: EventTemplate = {
created_at: Math.floor(Date.now() / 1000),
content: 'Event content',
kind: 1,
tags: [],
};
const signedEvent = signFromLocalstorage(unsignedEvent);
expect(signedEvent.sig).toBeTruthy();
});

test('assume no private key is found', () => {
const unsignedEvent: EventTemplate = {
created_at: Math.floor(Date.now() / 1000),
content: 'Event content',
kind: 1,
tags: [],
};
expect(() => {
const signedEvent = signFromLocalstorage(unsignedEvent);
}).toThrow();
});
});

describe('getting public key from local storage', () => {
afterEach(() => {
localStorage.removeItem('pleb_sk');
});
test('assume private key is found', () => {
localStorage.setItem('pleb_sk', generatePrivateKey());
const publicKey = getPublicKeyFromLocalstorage();
expect(publicKey).toBeTruthy();
});
test('assume no private key is found', () => {
expect(() => {
const publicKey = getPublicKeyFromLocalstorage();
}).toThrow();
});
});

0 comments on commit 42fc24b

Please sign in to comment.