Skip to content

Commit

Permalink
1. Add clarification about message to ExploreConnection
Browse files Browse the repository at this point in the history
2. ExploreConnection test
3. ExtensionConnection test
4. LookConnection test
  • Loading branch information
bryans99 committed Jan 8, 2025
1 parent b693a39 commit 6483368
Show file tree
Hide file tree
Showing 4 changed files with 333 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/v2/ExploreConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class ExploreConnection implements ILookerEmbedExplore {
*/

run() {
// This is correct!
this._connection.send('look:run')
}

Expand All @@ -51,6 +52,7 @@ export class ExploreConnection implements ILookerEmbedExplore {
*/

updateFilters(params: LookerEmbedFilterParams) {
// This is correct!
this._connection.send('look:filters:update', { filters: params })
}
}
116 changes: 116 additions & 0 deletions tests/v2/ExploreConnection.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
MIT License
Copyright (c) 2025 Looker Data Sciences, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

import mock from 'xhr-mock'
import type { ChattyHostBuilder } from '@looker/chatty'
import type {
CookielessCallback,
CookielessRequestInit,
GenerateTokensCallback,
LookerAuthConfig,
} from '../../src/types'
import { LookerEmbedExSDK } from '../../src/v2/LookerEmbedExSDK'
import type { EmbedConnection } from '../../src/v2/EmbedConnection'
import {
MockChattyHost,
MockChattyHostConnection,
MockHostBuilder,
waitFor,
} from './test_utils'

describe('ExploreConnection', () => {
let mockChattyHostConnection: MockChattyHostConnection
let mockChattyHost: MockChattyHost
let mockHostBuilder: MockHostBuilder

const getExploreConnection = async (
options: {
apiHost?: string
auth?: string | LookerAuthConfig
acquireSession?: string | CookielessRequestInit | CookielessCallback
generateTokens?: string | CookielessRequestInit | GenerateTokensCallback
} = {}
) => {
const {
apiHost = 'myhost.com',
auth,
acquireSession,
generateTokens,
} = options
const createChattyBuilder = (url: string) => {
mockHostBuilder._url = url
return mockHostBuilder as unknown as ChattyHostBuilder
}
const sdk = new LookerEmbedExSDK(createChattyBuilder)
if (acquireSession && generateTokens) {
sdk.initCookieless(apiHost, acquireSession, generateTokens)
} else {
sdk.init(apiHost, auth)
}
const connection = (await sdk
.preload()
.build()
.connect()) as EmbedConnection
return connection.asExploreConnection()
}

beforeEach(() => {
mock.setup()
mockChattyHostConnection = new MockChattyHostConnection()
mockChattyHost = new MockChattyHost()
mockChattyHost._hostConnection = mockChattyHostConnection
mockHostBuilder = new MockHostBuilder()
mockHostBuilder._chattyHost = mockChattyHost
})

afterEach(() => mock.teardown())

it('runs an explore', async () => {
const chattySendSpy = spyOn(
mockChattyHostConnection,
'send'
).and.callThrough()
const connection = await getExploreConnection()
connection.run()
await waitFor(() => chattySendSpy.calls.count() > 0)
expect(chattySendSpy).toHaveBeenCalledWith('look:run', undefined)
})

it('updates explore filters', async () => {
const chattySendSpy = spyOn(
mockChattyHostConnection,
'send'
).and.callThrough()
const connection = await getExploreConnection()
connection.updateFilters({ state: 'Califonia' })
await waitFor(() => chattySendSpy.calls.count() > 0)
expect(chattySendSpy).toHaveBeenCalledWith('look:filters:update', {
filters: {
state: 'Califonia',
},
})
})
})
99 changes: 99 additions & 0 deletions tests/v2/ExtensionConnection.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
MIT License
Copyright (c) 2025 Looker Data Sciences, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

import mock from 'xhr-mock'
import type { ChattyHostBuilder } from '@looker/chatty'
import type {
CookielessCallback,
CookielessRequestInit,
GenerateTokensCallback,
LookerAuthConfig,
} from '../../src/types'
import { LookerEmbedExSDK } from '../../src/v2/LookerEmbedExSDK'
import type { EmbedConnection } from '../../src/v2/EmbedConnection'
import {
MockChattyHost,
MockChattyHostConnection,
MockHostBuilder,
waitFor,
} from './test_utils'

describe('ExtensionConnection', () => {
let mockChattyHostConnection: MockChattyHostConnection
let mockChattyHost: MockChattyHost
let mockHostBuilder: MockHostBuilder

const getExtensionConnection = async (
options: {
apiHost?: string
auth?: string | LookerAuthConfig
acquireSession?: string | CookielessRequestInit | CookielessCallback
generateTokens?: string | CookielessRequestInit | GenerateTokensCallback
} = {}
) => {
const {
apiHost = 'myhost.com',
auth,
acquireSession,
generateTokens,
} = options
const createChattyBuilder = (url: string) => {
mockHostBuilder._url = url
return mockHostBuilder as unknown as ChattyHostBuilder
}
const sdk = new LookerEmbedExSDK(createChattyBuilder)
if (acquireSession && generateTokens) {
sdk.initCookieless(apiHost, acquireSession, generateTokens)
} else {
sdk.init(apiHost, auth)
}
const connection = (await sdk
.preload()
.build()
.connect()) as EmbedConnection
return connection.asExtensionConnection()
}

beforeEach(() => {
mock.setup()
mockChattyHostConnection = new MockChattyHostConnection()
mockChattyHost = new MockChattyHost()
mockChattyHost._hostConnection = mockChattyHostConnection
mockHostBuilder = new MockHostBuilder()
mockHostBuilder._chattyHost = mockChattyHost
})

afterEach(() => mock.teardown())

it('gets an extension connection', async () => {
const chattySendSpy = spyOn(
mockChattyHostConnection,
'send'
).and.callThrough()
const connection = await getExtensionConnection()
expect(connection).toBeDefined()
})
})
116 changes: 116 additions & 0 deletions tests/v2/LookConnection.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
MIT License
Copyright (c) 2025 Looker Data Sciences, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

import mock from 'xhr-mock'
import type { ChattyHostBuilder } from '@looker/chatty'
import type {
CookielessCallback,
CookielessRequestInit,
GenerateTokensCallback,
LookerAuthConfig,
} from '../../src/types'
import { LookerEmbedExSDK } from '../../src/v2/LookerEmbedExSDK'
import type { EmbedConnection } from '../../src/v2/EmbedConnection'
import {
MockChattyHost,
MockChattyHostConnection,
MockHostBuilder,
waitFor,
} from './test_utils'

describe('LookConnection', () => {
let mockChattyHostConnection: MockChattyHostConnection
let mockChattyHost: MockChattyHost
let mockHostBuilder: MockHostBuilder

const getLookConnection = async (
options: {
apiHost?: string
auth?: string | LookerAuthConfig
acquireSession?: string | CookielessRequestInit | CookielessCallback
generateTokens?: string | CookielessRequestInit | GenerateTokensCallback
} = {}
) => {
const {
apiHost = 'myhost.com',
auth,
acquireSession,
generateTokens,
} = options
const createChattyBuilder = (url: string) => {
mockHostBuilder._url = url
return mockHostBuilder as unknown as ChattyHostBuilder
}
const sdk = new LookerEmbedExSDK(createChattyBuilder)
if (acquireSession && generateTokens) {
sdk.initCookieless(apiHost, acquireSession, generateTokens)
} else {
sdk.init(apiHost, auth)
}
const connection = (await sdk
.preload()
.build()
.connect()) as EmbedConnection
return connection.asLookConnection()
}

beforeEach(() => {
mock.setup()
mockChattyHostConnection = new MockChattyHostConnection()
mockChattyHost = new MockChattyHost()
mockChattyHost._hostConnection = mockChattyHostConnection
mockHostBuilder = new MockHostBuilder()
mockHostBuilder._chattyHost = mockChattyHost
})

afterEach(() => mock.teardown())

it('runs a look', async () => {
const chattySendSpy = spyOn(
mockChattyHostConnection,
'send'
).and.callThrough()
const connection = await getLookConnection()
connection.run()
await waitFor(() => chattySendSpy.calls.count() > 0)
expect(chattySendSpy).toHaveBeenCalledWith('look:run', undefined)
})

it('updateslook filters', async () => {
const chattySendSpy = spyOn(
mockChattyHostConnection,
'send'
).and.callThrough()
const connection = await getLookConnection()
connection.updateFilters({ state: 'Califonia' })
await waitFor(() => chattySendSpy.calls.count() > 0)
expect(chattySendSpy).toHaveBeenCalledWith('look:filters:update', {
filters: {
state: 'Califonia',
},
})
})
})

0 comments on commit 6483368

Please sign in to comment.