Skip to content

Commit

Permalink
fix(configuration): make targetProperty a function
Browse files Browse the repository at this point in the history
  • Loading branch information
emkis committed Jun 1, 2022
1 parent 6c4aca3 commit cebee44
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/configuration/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const defaultConfigurations = {
debugContext: false,
},
events: {
targetProperty: window.dataLayer,
targetProperty: () => window.dataLayer,
},
} as const

Expand Down
14 changes: 7 additions & 7 deletions src/shared/data-layer/data-layer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { WarningError } from '@/shared/error'
import { configuration, Configurations } from '@/configuration'

type DataLayerFactory = Partial<{
targetProperty: EventProperties[] | null | object
targetProperty: () => EventProperties[] | null | object
}>

function makeDataLayer({ targetProperty = [] }: DataLayerFactory = {}) {
function makeDataLayer({ targetProperty = () => [] }: DataLayerFactory = {}) {
const mockConfiguration: Configurations = {
logger: configuration.defaults().logger,
events: {
targetProperty: targetProperty as EventProperties[],
targetProperty: targetProperty as () => EventProperties[],
},
}

Expand Down Expand Up @@ -71,22 +71,22 @@ it('should throw error if is server side', () => {
})

it('should throw error if targetProperty is not available', () => {
const { dataLayer } = makeDataLayer({ targetProperty: null })
const { dataLayer } = makeDataLayer({ targetProperty: () => null })
expect(dataLayer.addEvent).toThrowError(WarningError)
})

it('should throw error if targetProperty is not an array', () => {
const { dataLayer } = makeDataLayer({ targetProperty: {} })
const { dataLayer } = makeDataLayer({ targetProperty: () => ({}) })
expect(dataLayer.addEvent).toThrowError(WarningError)
})

it('should be able to push event when targetProperty is available', () => {
const { dataLayer, mockConfiguration } = makeDataLayer({
targetProperty: null,
targetProperty: () => null,
})

expect(dataLayer.addEvent).toThrowError(WarningError)
mockConfiguration.events.targetProperty = []
mockConfiguration.events.targetProperty = () => []
const eventPayload: EventProperties = { some: 'data' }

expect(() => {
Expand Down

0 comments on commit cebee44

Please sign in to comment.