Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chrome.storage.local.get should return a deep copy of objects #133

Open
carbureted opened this issue Jan 29, 2021 · 0 comments
Open

chrome.storage.local.get should return a deep copy of objects #133

carbureted opened this issue Jan 29, 2021 · 0 comments

Comments

@carbureted
Copy link

carbureted commented Jan 29, 2021

Presently it's possible for test behavior and real code behavior to differ because changes to objects returned by chrome.storage.local.get can be "persistently" modified in functions that do not save the result to chrome.storage.

interface Thing {
  array: string[]
}

function saveThing(thing: Thing) {
  return new Promise((resolve) => {
    chrome.storage.local.set({ thing: thing }, () => {
      resolve(true)
    })
  })
}

function getThing(): Promise<Thing> {
  return new Promise((resolve) => {
    chrome.storage.local.get('thing', (data: { thing: Thing }) => {
      resolve(data.thing)
    })
  })
}

function doSomethingWithThing(thing: Thing) {
  thing.array = thing.array.map((string) => {
    return 'bar'
  })
}

describe('Thing should work', () => {
  it('thing works', async () => {
    await saveThing({ array: ['foo'] })
    let thing = await getThing()
    expect(thing.array[0]).toEqual('foo')

    doSomethingWithThing(thing)

    thing = await getThing()
    expect(thing.array[0]).toEqual('foo')
  })
})

Expected: "foo"
Received: "bar"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant