acvetkov / sinon-chrome

Testing chrome extensions with Node.js
ISC License
439 stars 48 forks source link

Storage API example #105

Closed davestewart closed 4 years ago

davestewart commented 4 years ago

Hi there,

Feel free to close this ticket as it's a question rather than support, but...

I'm new to Sinon and have done some basic tutorials to get the very basics, but how would one write a Jest test to check storage has been updated?

My use case is that I'm writing an upgrade routine to migrate between extension versions. It has to read, transform, and finally write stored user data to storage, so that the old schema is updated to the new schema (in a manner similar Indexed DB's schema upgrade routine).

Because I'm using a wrapped storage API, I'm not 100% how I would set something like this up.

Essentially what I want in normal test land, looks like the following:

// migrate.js

import { storage } from '@/api/storage'

export async function migrate () {
  // v1 data
  const foo = await storage.get('foo')

  // v2 data
  await storage.remove('foo')
  await storage.set('bar', foo)
}
// migrate.spec.js

import { storage } from '@/api/storage'
import { migrate } from '@/services/migrate'

describe('user data is migrated from v1 to v2', async function () {
  // values
  const v1 = { foo: 1 }
  const v2 = { bar: 1 }

  // setup storage
  await storage.set(v1)

  // migrate to v2
  await migrate()

  // check migration
  const actual = await storage.get(null)
  expect(actual).toBe(v2)
})

Would it be possible to get an example?

I hope, using that, it will help me connect the dots conceptually.

Thanks so much, in advance.

davestewart commented 4 years ago

OK, I figured it all out in the end. Thanks.

nikoisonfire commented 3 years ago

OK, I figured it all out in the end. Thanks.

At least post your solution lmao.

davestewart commented 3 years ago

I think it was more figuring out how Sinon works, vs any particular solution :)