Skip to content

Commit

Permalink
fix: fix readme + exception on wrong storage
Browse files Browse the repository at this point in the history
.
  • Loading branch information
kangoo13 committed May 10, 2023
1 parent 1c69348 commit 59f60b0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "puppeteer-extra-plugin-session-persistence",
"version": "1.0.1",
"version": "1.0.2",
"description": "A puppeteer-extra plugin to persist sessions.",
"main": "dist/index.cjs.js",
"module": "dist/index.esm.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class FileSystemStorage implements Storage {
private cookiesFile: string;

get name() {
return 'file-system';
return 'filesystem';
}

constructor(fileSystemStorageOptions: FileSystemStorageOptions = {}) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import test from 'ava';
import { createStorage, StorageConfig } from './types';
import { RedisStorage } from './storage/redisStorage';
import { FileSystemStorage } from './storage/fileSystemStorage';
import { InMemoryStorage } from './storage/inMemoryStorage';

test('createStorage should create RedisStorage instance', (t) => {
const config: StorageConfig = {
name: RedisStorage.name.toLowerCase(),
options: {
host: 'localhost',
port: 6379,
password: 'password',
},
};

const storage = createStorage(config);
t.true(storage instanceof RedisStorage);
});

test('createStorage should create FileSystemStorage instance', (t) => {
const config: StorageConfig = {
name: FileSystemStorage.name.toLowerCase(),
options: {
localStorageDataFile: 'localStorageData.json',
cookiesFile: 'cookies.json',
},
};

const storage = createStorage(config);
t.true(storage instanceof FileSystemStorage);
});

test('createStorage should create InMemoryStorage instance', (t) => {
const config: StorageConfig = {
name: InMemoryStorage.name.toLowerCase(),
options: undefined,
};

const storage = createStorage(config);
t.true(storage instanceof InMemoryStorage);
});

test('createStorage should throw an error for unknown storage name', (t) => {
const config: StorageConfig = {
name: 'unknown',
options: undefined,
};

const error = t.throws(() => {
createStorage(config);
});

t.is(error.message, 'Unknown storage name: unknown');
});
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ export function createStorage(opts: StorageConfig) {
return new FileSystemStorage(opts.options as FileSystemStorageOptions);
case InMemoryStorage.name.toLowerCase():
return new InMemoryStorage();
default:
throw new Error(`Unknown storage name: ${opts.name}`);
}

return new FileSystemStorage();
}

export type Cookie = NetworkCookie;

0 comments on commit 59f60b0

Please sign in to comment.