1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/app/scripts/lib/network-store.js
Mark Stacey eadeaa7883 End-to-end test state fixtures (#7663)
* Add network store for testing

An alternative persistent state store has been created for use with e2e
tests. Instead of reading state from disk, it tries to load state from
a local fixture server running on port `12345` and serving state from
the path `/state.json`, and returns a blank state otherwise.

* Add e2e test fixture server

A fixture server has been added for serving background state, which the
background will read upon startup as part of restoring persisted state.

The `signature-request` e2e test has been updated to use a fixture to
bypass the registration step. The fixture used (`imported-account`) was
generated by pausing midway through that test run
2019-12-11 09:26:20 -08:00

63 lines
1.3 KiB
JavaScript

const log = require('loglevel')
const FIXTURE_SERVER_HOST = 'localhost'
const FIXTURE_SERVER_PORT = 12345
const FIXTURE_SERVER_URL = `http://${FIXTURE_SERVER_HOST}:${FIXTURE_SERVER_PORT}/state.json`
/**
* A read-only network-based storage wrapper
*/
class ReadOnlyNetworkStore {
constructor () {
this._initialized = false
this._initializing = this._init()
this._state = undefined
}
/**
* Declares this store as compatible with the current browser
*/
isSupported = true
/**
* Initializes by loading state from the network
*/
async _init () {
try {
const response = await fetch(FIXTURE_SERVER_URL)
if (response.ok) {
this._state = await response.json()
}
} catch (error) {
log.debug(`Error loading network state: '${error.message}'`)
} finally {
this._initialized = true
}
}
/**
* Returns state
* @return {Promise<object>}
*/
async get () {
if (!this._initialized) {
await this._initializing
}
return this._state
}
/**
* Set state
* @param {object} state - The state to set
* @return {Promise<void>}
*/
async set (state) {
if (!this._initialized) {
await this._initializing
}
this._state = state
}
}
module.exports = ReadOnlyNetworkStore