68 lines
2.0 KiB
Plaintext
68 lines
2.0 KiB
Plaintext
import { get } from "svelte/store"
|
|
import { API } from "api"
|
|
|
|
jest.mock("api")
|
|
|
|
import { SOME_DATASOURCE, SAVE_DATASOURCE } from "./fixtures/datasources"
|
|
|
|
import { createDatasourcesStore } from "../datasources"
|
|
import { queries } from '../queries'
|
|
|
|
describe("Datasources Store", () => {
|
|
let store = createDatasourcesStore()
|
|
|
|
beforeEach(async () => {
|
|
API.getDatasources.mockReturnValue({ json: () => [SOME_DATASOURCE]})
|
|
await store.init()
|
|
})
|
|
|
|
it("Initialises correctly", async () => {
|
|
API.getDatasources.mockReturnValue({ json: () => [SOME_DATASOURCE]})
|
|
|
|
await store.init()
|
|
expect(get(store)).toEqual({ list: [SOME_DATASOURCE], selected: null})
|
|
})
|
|
|
|
it("fetches all the datasources and updates the store", async () => {
|
|
API.getDatasources.mockReturnValue({ json: () => [SOME_DATASOURCE] })
|
|
|
|
await store.fetch()
|
|
expect(get(store)).toEqual({ list: [SOME_DATASOURCE], selected: null })
|
|
})
|
|
|
|
it("selects a datasource", async () => {
|
|
store.select(SOME_DATASOURCE._id)
|
|
|
|
expect(get(store).select).toEqual(SOME_DATASOURCE._id)
|
|
})
|
|
|
|
it("resets the queries store when new datasource is selected", async () => {
|
|
|
|
await store.select(SOME_DATASOURCE._id)
|
|
const queriesValue = get(queries)
|
|
expect(queriesValue.selected).toEqual(null)
|
|
})
|
|
|
|
it("saves the datasource, updates the store and returns status message", async () => {
|
|
API.createDatasource.mockReturnValue({ status: 200, json: () => SAVE_DATASOURCE})
|
|
|
|
await store.save({
|
|
name: 'CoolDB',
|
|
source: 'REST',
|
|
config: SOME_DATASOURCE[0].config
|
|
|
|
})
|
|
|
|
expect(get(store).list).toEqual(expect.arrayContaining([SAVE_DATASOURCE.datasource]))
|
|
})
|
|
it("deletes a datasource, updates the store and returns status message", async () => {
|
|
API.getDatasources.mockReturnValue({ json: () => SOME_DATASOURCE})
|
|
|
|
await store.fetch()
|
|
|
|
API.deleteDatasource.mockReturnValue({status: 200, message: 'Datasource deleted.'})
|
|
|
|
await store.delete(SOME_DATASOURCE[0])
|
|
expect(get(store)).toEqual({ list: [], selected: null})
|
|
})
|
|
}) |