Skip to content

Commit

Permalink
chore: moving component tests (#942)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhousley authored Apr 3, 2024
1 parent daa41f4 commit 19e076e
Show file tree
Hide file tree
Showing 22 changed files with 170 additions and 148 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ module.exports = {
}
},
{
files: ['src/**/*.test.js', 'src/**/*.component-test.js', 'src/**/__mocks__/**/*'],
files: ['src/**/*.test.js', 'src/**/__mocks__/**/*', 'tests/components/**/*'],
env: {
browser: true,
node: true,
Expand Down
1 change: 0 additions & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ module.exports = function (api, ...args) {

const ignore = [
'**/*.test.js',
'**/*.component-test.js',
'**/__mocks__/*.js'
]
const presets = [
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = {
{
...commonConfig,
displayName: 'component',
testMatch: ['<rootDir>/src/**/?(*.)+(component-test).[tj]s?(x)']
testMatch: ['<rootDir>/tests/components/**/?(*.)+(test).[tj]s?(x)']
}
]
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { faker } from '@faker-js/faker'
import { FEATURE_NAMES } from '../features/features'
import { SUPPORTABILITY_METRIC_CHANNEL } from '../../features/metrics/constants'
import { setAPI } from './api'
import { setInfo, setConfiguration, setRuntime, getRuntime, getInfo } from '../../common/config/config'
import { ee } from '../../common/event-emitter/contextual-ee'
import * as drainModule from '../../common/drain/drain'
import * as runtimeModule from '../../common/constants/runtime'
import * as asyncApiModule from './apiAsync'
import * as windowLoadModule from '../../common/window/load'
import * as handleModule from '../../common/event-emitter/handle'
import { SR_EVENT_EMITTER_TYPES } from '../../features/session_replay/constants'
import { FEATURE_NAMES } from '../../src/loaders/features/features'
import { SUPPORTABILITY_METRIC_CHANNEL } from '../../src/features/metrics/constants'
import { setAPI } from '../../src/loaders/api/api'
import { setInfo, setConfiguration, setRuntime, getRuntime, getInfo } from '../../src/common/config/config'
import { ee } from '../../src/common/event-emitter/contextual-ee'
import * as drainModule from '../../src/common/drain/drain'
import * as runtimeModule from '../../src/common/constants/runtime'
import * as asyncApiModule from '../../src/loaders/api/apiAsync'
import * as windowLoadModule from '../../src/common/window/load'
import * as handleModule from '../../src/common/event-emitter/handle'
import { SR_EVENT_EMITTER_TYPES } from '../../src/features/session_replay/constants'

describe('setAPI', () => {
let agentId
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { faker } from '@faker-js/faker'
import { FEATURE_NAMES } from '../features/features'
import { CUSTOM_METRIC_CHANNEL } from '../../features/metrics/constants'
import { setAPI } from './apiAsync'
import { setInfo, setConfiguration, setRuntime, getRuntime } from '../../common/config/config'
import { ee } from '../../common/event-emitter/contextual-ee'
import * as registerHandlerModule from '../../common/event-emitter/register-handler'
import * as handleModule from '../../common/event-emitter/handle'
import { FEATURE_NAMES } from '../../src/loaders/features/features'
import { CUSTOM_METRIC_CHANNEL } from '../../src/features/metrics/constants'
import { setAPI } from '../../src/loaders/api/apiAsync'
import { setInfo, setConfiguration, setRuntime, getRuntime } from '../../src/common/config/config'
import { ee } from '../../src/common/event-emitter/contextual-ee'
import * as registerHandlerModule from '../../src/common/event-emitter/register-handler'
import * as handleModule from '../../src/common/event-emitter/handle'

describe('setAPI', () => {
let agentId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ beforeEach(() => {
mockNREUM = {}
runtime = {}

jest.doMock('../window/nreum', () => ({
jest.doMock('../../src/common/window/nreum', () => ({
__esModule: true,
gosNREUM: jest.fn(() => mockNREUM)
}))

jest.doMock('../config/config', () => ({
jest.doMock('../../src/common/config/config', () => ({
__esModule: true,
getRuntime: jest.fn(() => runtime)
}))
Expand All @@ -25,23 +25,23 @@ afterEach(() => {

describe('global event-emitter', () => {
test('it sets the global event-emitter on window.NREUM when it does not already exist', async () => {
const { ee } = await import('./contextual-ee')
const { ee } = await import('../../src/common/event-emitter/contextual-ee')

expect(ee).toEqual(mockNREUM.ee)
})

test('it does not set the global event-emitter on window.NREUM when it already exists', async () => {
mockNREUM.ee = {}

const { ee } = await import('./contextual-ee')
const { ee } = await import('../../src/common/event-emitter/contextual-ee')

expect(ee).not.toEqual(mockNREUM.ee)
})
})

describe('scoping event-emitter', () => {
test('it creates a new child event-emitter', async () => {
const { ee } = await import('./contextual-ee')
const { ee } = await import('../../src/common/event-emitter/contextual-ee')

const childName = faker.string.uuid()
const result = ee.get(childName)
Expand All @@ -54,7 +54,7 @@ describe('scoping event-emitter', () => {
test('it creates a child event-emitter with an isolated backlog', async () => {
const childName = faker.string.alphanumeric(16)

jest.doMock('../config/config', () => ({
jest.doMock('../../src/common/config/config', () => ({
__esModule: true,
getRuntime: jest.fn(id => {
if (id === childName) {
Expand All @@ -65,7 +65,7 @@ describe('scoping event-emitter', () => {
})
}))

const { ee } = await import('./contextual-ee')
const { ee } = await import('../../src/common/event-emitter/contextual-ee')
const result = ee.get(childName)

expect(ee.backlog).not.toBe(result.backlog)
Expand All @@ -74,7 +74,7 @@ describe('scoping event-emitter', () => {

describe('event-emitter context', () => {
test('it returns a new context', async () => {
const { ee } = await import('./contextual-ee')
const { ee } = await import('../../src/common/event-emitter/contextual-ee')

const result = ee.context()

Expand All @@ -84,15 +84,15 @@ describe('event-emitter context', () => {
})

test('it returns the same context', async () => {
const { ee } = await import('./contextual-ee')
const { ee } = await import('../../src/common/event-emitter/contextual-ee')

const result = ee.context()

expect(result).toBe(ee.context(result))
})

test('it adds the context to the provided object', async () => {
const { ee } = await import('./contextual-ee')
const { ee } = await import('../../src/common/event-emitter/contextual-ee')

const obj = {}
const result = ee.context(obj)
Expand All @@ -104,7 +104,7 @@ describe('event-emitter context', () => {

describe('event-emitter buffer', () => {
test('it should create a new buffer for the given group', async () => {
const { ee } = await import('./contextual-ee')
const { ee } = await import('../../src/common/event-emitter/contextual-ee')
const eventType = faker.string.uuid()
const group = faker.string.uuid()

Expand All @@ -117,7 +117,7 @@ describe('event-emitter buffer', () => {
})

test('it should default group to "feature"', async () => {
const { ee } = await import('./contextual-ee')
const { ee } = await import('../../src/common/event-emitter/contextual-ee')
const eventType = faker.string.uuid()

ee.buffer([eventType])
Expand All @@ -129,7 +129,7 @@ describe('event-emitter buffer', () => {
})

test('it should not create buffer if event-emitter is aborted', async () => {
const { ee } = await import('./contextual-ee')
const { ee } = await import('../../src/common/event-emitter/contextual-ee')
const eventType = faker.string.uuid()
const group = faker.string.uuid()

Expand All @@ -144,7 +144,7 @@ describe('event-emitter buffer', () => {
})

test('it should empty the backlog on abort as opposed to replacing it', async () => {
const { ee } = await import('./contextual-ee')
const { ee } = await import('../../src/common/event-emitter/contextual-ee')

const backlog = {
api: ['foo', 'bar', 'baz']
Expand All @@ -157,8 +157,8 @@ describe('event-emitter buffer', () => {
})

test('it should not buffer after drain', async () => {
const { ee } = await import('./contextual-ee')
const { drain } = await import('../drain/drain')
const { ee } = await import('../../src/common/event-emitter/contextual-ee')
const { drain } = await import('../../src/common/drain/drain')
const mockListener = jest.fn()
const eventType = faker.string.uuid()
const eventArgs = ['a', 'b', 'c']
Expand Down Expand Up @@ -195,7 +195,7 @@ describe('event-emitter buffer', () => {

describe('event-emitter abort', () => {
test('it aborts if there is an API backlog', async () => {
const { ee } = await import('./contextual-ee')
const { ee } = await import('../../src/common/event-emitter/contextual-ee')

ee.backlog = {
api: ['foo', 'bar', 'baz']
Expand All @@ -207,7 +207,7 @@ describe('event-emitter abort', () => {
})

test('it aborts if there is a feature backlog', async () => {
const { ee } = await import('./contextual-ee')
const { ee } = await import('../../src/common/event-emitter/contextual-ee')

ee.backlog = {
feature: ['foo', 'bar', 'baz']
Expand All @@ -221,7 +221,7 @@ describe('event-emitter abort', () => {

describe('event-emitter emit', () => {
test('should execute the listener', async () => {
const { ee } = await import('./contextual-ee')
const { ee } = await import('../../src/common/event-emitter/contextual-ee')
const mockListener = jest.fn()
const eventType = faker.string.uuid()
const eventArgs = ['a', 'b', 'c']
Expand All @@ -233,7 +233,7 @@ describe('event-emitter emit', () => {
})

test('should not execute the listener after removal', async () => {
const { ee } = await import('./contextual-ee')
const { ee } = await import('../../src/common/event-emitter/contextual-ee')
const mockListener = jest.fn()
const eventType = faker.string.uuid()
const eventArgs = ['a', 'b', 'c']
Expand All @@ -247,7 +247,7 @@ describe('event-emitter emit', () => {
})

test('should return early if global event-emitter is aborted', async () => {
const { ee } = await import('./contextual-ee')
const { ee } = await import('../../src/common/event-emitter/contextual-ee')
const mockListener = jest.fn()
const eventType = faker.string.uuid()
const eventArgs = ['a', 'b', 'c']
Expand All @@ -263,7 +263,7 @@ describe('event-emitter emit', () => {
})

test('should still emit if global event-emitter is aborted but force flag is true', async () => {
const { ee } = await import('./contextual-ee')
const { ee } = await import('../../src/common/event-emitter/contextual-ee')
const scopeEE = ee.get(faker.string.uuid())
const mockScopeListener = jest.fn()
const eventType = faker.string.uuid()
Expand All @@ -280,7 +280,7 @@ describe('event-emitter emit', () => {
})

test('should bubble the event if bubble flag is true', async () => {
const { ee } = await import('./contextual-ee')
const { ee } = await import('../../src/common/event-emitter/contextual-ee')
const scopeEE = ee.get(faker.string.uuid())
const mockListener = jest.fn()
const mockScopeListener = jest.fn()
Expand All @@ -296,7 +296,7 @@ describe('event-emitter emit', () => {
})

test('should not bubble the event if bubble flag is false', async () => {
const { ee } = await import('./contextual-ee')
const { ee } = await import('../../src/common/event-emitter/contextual-ee')
const scopeEE = ee.get(faker.string.uuid())
const mockListener = jest.fn()
const mockScopeListener = jest.fn()
Expand All @@ -312,7 +312,7 @@ describe('event-emitter emit', () => {
})

test('should buffer the event on the scoped event-emitter', async () => {
const { ee } = await import('./contextual-ee')
const { ee } = await import('../../src/common/event-emitter/contextual-ee')
const scopeEE = ee.get(faker.string.uuid())
const mockListener = jest.fn()
const mockScopeListener = jest.fn()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InteractionTimer } from './interaction-timer'
import { InteractionTimer } from '../../src/common/timer/interaction-timer'

jest.useFakeTimers()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Aggregator } from '../../../common/aggregate/aggregator'
import { Aggregate } from '.'
import { ee } from '../../../common/event-emitter/contextual-ee'
import { SUPPORTABILITY_METRIC, CUSTOM_METRIC } from '../constants'
import { Aggregator } from '../../../src/common/aggregate/aggregator'
import { Aggregate } from '../../../src/features/metrics/aggregate'
import { ee } from '../../../src/common/event-emitter/contextual-ee'
import { SUPPORTABILITY_METRIC, CUSTOM_METRIC } from '../../../src/features/metrics/constants'

const METRIC_NAME = 'test'
const agg = new Aggregator({ agentIdentifier: 'abcd', ee })
Expand All @@ -18,7 +18,7 @@ const createAndStoreMetric = (value, isSupportability) => {
method(METRIC_NAME, value)
}

jest.mock('../../../common/config/config', () => ({
jest.mock('../../../src/common/config/config', () => ({
__esModule: true,
isConfigured: jest.fn().mockReturnValue(true),
getConfigurationValue: jest.fn().mockReturnValue(undefined),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { faker } from '@faker-js/faker'
import * as configModule from '../config/config'
import * as urlProtocolModule from '../url/protocol'
import * as consolModule from './console'
import * as obfuscateModule from './obfuscate'

jest.mock('../config/config')
jest.mock('../context/shared-context')
jest.mock('../url/protocol')
jest.mock('./console')
import * as configModule from '../../src/common/config/config'
import * as urlProtocolModule from '../../src/common/url/protocol'
import * as consolModule from '../../src/common/util/console'
import * as obfuscateModule from '../../src/common/util/obfuscate'

jest.mock('../../src/common/config/config')
jest.mock('../../src/common/context/shared-context')
jest.mock('../../src/common/url/protocol')
jest.mock('../../src/common/util/console')

let agentIdentifier
const rules = [{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Aggregator } from '../../../common/aggregate/aggregator'
import { ee } from '../../../common/event-emitter/contextual-ee'
import { setRuntime } from '../../../common/config/config'
import { VITAL_NAMES } from '../../../common/vitals/constants'
import { Aggregator } from '../../../src/common/aggregate/aggregator'
import { ee } from '../../../src/common/event-emitter/contextual-ee'
import { setRuntime } from '../../../src/common/config/config'
import { VITAL_NAMES } from '../../../src/common/vitals/constants'

// Note: these callbacks fire right away unlike the real web-vitals API which are async-on-trigger
jest.mock('web-vitals/attribution', () => ({
Expand Down Expand Up @@ -32,7 +32,7 @@ jest.mock('web-vitals/attribution', () => ({
}))
}))
let triggerVisChange
jest.mock('../../../common/window/page-visibility', () => ({
jest.mock('../../../src/common/window/page-visibility', () => ({
subscribeToVisibilityChange: jest.fn(cb => { triggerVisChange ??= cb })
}))

Expand All @@ -48,7 +48,7 @@ const agentId = 'abcd'
describe('pvt aggregate tests', () => {
beforeEach(async () => {
triggerVisChange = undefined
jest.doMock('../../../common/util/feature-flags', () => ({
jest.doMock('../../../src/common/util/feature-flags', () => ({
__esModule: true,
activatedFeatures: { [agentId]: { pvt: 1 } }
}))
Expand All @@ -58,7 +58,7 @@ describe('pvt aggregate tests', () => {
// }))

setRuntime(agentId, {})
const { Aggregate } = await import('.')
const { Aggregate } = await import('../../../src/features/page_view_timing/aggregate')

global.navigator.connection = {
type: 'cellular',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PREFIX } from './constants'
import { SessionEntity } from './session-entity'
import { PREFIX } from '../../src/common/session/constants'
import { SessionEntity } from '../../src/common/session/session-entity'

const agentIdentifier = 'test_agent_identifier'
const key = 'test_key'
Expand Down Expand Up @@ -47,12 +47,12 @@ const model = {
custom: {}
}

jest.mock('../timer/timer')
jest.mock('../timer/interaction-timer')
jest.mock('../../src/common/timer/timer')
jest.mock('../../src/common/timer/interaction-timer')
jest.useFakeTimers()

const mockBrowserScope = jest.fn().mockImplementation(() => true)
jest.mock('../constants/runtime', () => ({
jest.mock('../../src/common/constants/runtime', () => ({
__esModule: true,
get isBrowserScope () {
return mockBrowserScope()
Expand Down
Loading

0 comments on commit 19e076e

Please sign in to comment.