Skip to content

Commit

Permalink
feat: add unit test to shadows generator
Browse files Browse the repository at this point in the history
  • Loading branch information
aliceinapple committed Jun 14, 2024
1 parent f0275a5 commit f42fe6f
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ import { toColorName } from '@atls/figma-utils'
import { toColorString } from '@atls/figma-utils'
import { walk } from '@atls/figma-utils'

interface Shadow {
offsetX: number
offsetY: number
radius: number
spreadRadius?: number
color: string
}
import { Shadow } from './interfaces.js'

export class FigmaThemeShadowsGenerator extends FigmaThemeGenerator {
readonly name = 'shadows'
Expand Down
7 changes: 7 additions & 0 deletions theme/theme-shadows-generator/src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface Shadow {
offsetX: number
offsetY: number
radius: number
spreadRadius?: number
color: string
}
93 changes: 93 additions & 0 deletions theme/theme-shadows-generator/src/unit/generate-shadows.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { FigmaThemeShadowsGenerator } from '../FigmaThemeShadowsGenerator.js'

describe('FigmaThemeShadowsGenerator', () => {
let generator: FigmaThemeShadowsGenerator

beforeEach(() => {
generator = new FigmaThemeShadowsGenerator()
})

it('should correctly identify and return shadows', () => {
const nodes = [
{
type: 'DROP_SHADOW',
offset: { x: 10, y: 20 },
radius: 15,
spread: 5,
color: { r: 1, g: 0, b: 0, a: 0.5 },
},
{
type: 'INNER_SHADOW',
offset: { x: 5, y: 10 },
radius: 10,
color: { r: 0, g: 0, b: 0, a: 0.75 },
},
{
type: 'TEXT',
style: { fontSize: 16 },
},
]

const shadows = generator.getShadows(nodes)

expect(shadows).toEqual({
red: '10px 20px 15px 5px rgba(255, 0, 0, 0.50)',
black: '5px 10px 10px rgba(0, 0, 0, 0.75)',
})
})

it('should generate shadows correctly', () => {
const file = {
document: {
children: [
{
type: 'DROP_SHADOW',
offset: { x: 10, y: 20 },
radius: 15,
spread: 5,
color: { r: 1, g: 0, b: 0, a: 0.5 },
},
{
type: 'INNER_SHADOW',
offset: { x: 5, y: 10 },
radius: 10,
color: { r: 0, g: 0, b: 0, a: 0.75 },
},
],
},
}

// @ts-ignore
const result = generator.generate(file)

expect(result).toEqual({
name: 'shadows',
content: `export const shadows = {
"red": "10px 20px 15px 5px rgba(255, 0, 0, 0.50)",
"black": "5px 10px 10px rgba(0, 0, 0, 0.75)"
}`,
})
})

it('should return empty object if no shadows found', () => {
const nodes = [{ type: 'TEXT', style: { fontSize: 16 } }]

const shadows = generator.getShadows(nodes)

expect(shadows).toEqual({})

const file = {
document: {
children: nodes,
},
}

// @ts-ignore
const result = generator.generate(file)

expect(result).toEqual({
name: 'shadows',
content: `export const shadows = {}`,
})
})
})

0 comments on commit f42fe6f

Please sign in to comment.