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

🎨 [RUM-5100] Move away from testbuilder in test files - Pt 3 #2952

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 39 additions & 33 deletions packages/rum-core/src/domain/contexts/internalContext.spec.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,47 @@
import type { RelativeTime } from '@datadog/browser-core'
import { createRumSessionManagerMock, setup } from '../../../test'
import type { TestSetupBuilder } from '../../../test'
import { noop, type RelativeTime } from '@datadog/browser-core'
import { buildLocation } from '@datadog/browser-core/test'
import { createRumSessionManagerMock } from '../../../test'
import type { ActionContexts } from '../action/actionCollection'
import type { RumSessionManager } from '../rumSessionManager'
import { startInternalContext } from './internalContext'
import type { ViewContexts } from './viewContexts'
import type { UrlContexts } from './urlContexts'

describe('internal context', () => {
let setupBuilder: TestSetupBuilder
let viewContextsStub: Partial<ViewContexts>
let actionContextsStub: ActionContexts
let findUrlSpy: jasmine.Spy<UrlContexts['findUrl']>
let findSessionSpy: jasmine.Spy<RumSessionManager['findTrackedSession']>
let internalContext: ReturnType<typeof startInternalContext>
const viewContexts: ViewContexts = {
findView: jasmine.createSpy('findView').and.returnValue({
id: 'abcde',
name: 'foo',
}),
stop: noop,
}

beforeEach(() => {
viewContextsStub = {
findView: jasmine.createSpy('findView').and.returnValue({
id: 'abcde',
name: 'foo',
}),
}
actionContextsStub = {
findActionId: jasmine.createSpy('findActionId').and.returnValue('7890'),
}
setupBuilder = setup()
.withSessionManager(createRumSessionManagerMock().setId('456'))
.withViewContexts(viewContextsStub)
.withActionContexts(actionContextsStub)
.beforeBuild(({ applicationId, sessionManager, viewContexts, urlContexts, actionContexts }) => {
findUrlSpy = spyOn(urlContexts, 'findUrl').and.callThrough()
findSessionSpy = spyOn(sessionManager, 'findTrackedSession').and.callThrough()
internalContext = startInternalContext(applicationId, sessionManager, viewContexts, actionContexts, urlContexts)
})
})
const actionContexts: ActionContexts = {
findActionId: jasmine.createSpy('findActionId').and.returnValue('7890'),
}

const fakeLocation = buildLocation('/foo')

const urlContexts: UrlContexts = {
findUrl: () => ({
url: fakeLocation.href,
referrer: document.referrer,
}),
stop: noop,
}

function setupInternalContext(sessionManager: RumSessionManager) {
findSessionSpy = spyOn(sessionManager, 'findTrackedSession').and.callThrough()
findUrlSpy = spyOn(urlContexts, 'findUrl').and.callThrough()

return startInternalContext('appId', sessionManager, viewContexts, actionContexts, urlContexts)
}
N-Boutaib marked this conversation as resolved.
Show resolved Hide resolved

it('should return current internal context', () => {
const { fakeLocation } = setupBuilder.build()
const sessionManagerr = createRumSessionManagerMock().setId('456')
const internalContext = setupInternalContext(sessionManagerr)

expect(internalContext.get()).toEqual({
application_id: 'appId',
Expand All @@ -49,23 +53,25 @@ describe('internal context', () => {
id: 'abcde',
name: 'foo',
referrer: document.referrer,
url: fakeLocation.href!,
url: fakeLocation.href,
},
})
})

it("should return undefined if the session isn't tracked", () => {
setupBuilder.withSessionManager(createRumSessionManagerMock().setNotTracked()).build()
const sessionManagerr = createRumSessionManagerMock().setNotTracked()
N-Boutaib marked this conversation as resolved.
Show resolved Hide resolved
const internalContext = setupInternalContext(sessionManagerr)
expect(internalContext.get()).toEqual(undefined)
})

it('should return internal context corresponding to startTime', () => {
setupBuilder.build()
const sessionManagerr = createRumSessionManagerMock().setId('456')
N-Boutaib marked this conversation as resolved.
Show resolved Hide resolved
const internalContext = setupInternalContext(sessionManagerr)

internalContext.get(123)

expect(viewContextsStub.findView).toHaveBeenCalledWith(123)
expect(actionContextsStub.findActionId).toHaveBeenCalledWith(123 as RelativeTime)
expect(viewContexts.findView).toHaveBeenCalledWith(123 as RelativeTime)
expect(actionContexts.findActionId).toHaveBeenCalledWith(123 as RelativeTime)
expect(findUrlSpy).toHaveBeenCalledWith(123 as RelativeTime)
expect(findSessionSpy).toHaveBeenCalledWith(123 as RelativeTime)
})
Expand Down