|
| 1 | +import { readFile } from 'node:fs/promises'; |
| 2 | +import { beforeAll, describe, expect, it } from 'vitest'; |
| 3 | +import { parse } from '@terrazzo/parser'; |
| 4 | +import config from '../../../terrazzo.config.ts'; |
| 5 | +import { generateFigmaFiles } from '../generate.ts'; |
| 6 | + |
| 7 | +describe( 'Figma border collection files', () => { |
| 8 | + let files: ReturnType< typeof generateFigmaFiles >; |
| 9 | + let border: Record< |
| 10 | + string, |
| 11 | + Record< |
| 12 | + string, |
| 13 | + { |
| 14 | + $description?: string; |
| 15 | + $extensions?: Record< string, unknown >; |
| 16 | + } |
| 17 | + > |
| 18 | + >; |
| 19 | + |
| 20 | + beforeAll( async () => { |
| 21 | + const sources = await Promise.all( |
| 22 | + config.tokens.map( async ( filename: URL ) => ( { |
| 23 | + filename, |
| 24 | + src: await readFile( filename, 'utf8' ), |
| 25 | + } ) ) |
| 26 | + ); |
| 27 | + const { resolver } = await parse( sources, { config } ); |
| 28 | + files = generateFigmaFiles( resolver ); |
| 29 | + border = JSON.parse( |
| 30 | + await readFile( |
| 31 | + new URL( '../../../tokens/border.json', import.meta.url ), |
| 32 | + 'utf8' |
| 33 | + ) |
| 34 | + )[ 'wpds-border' ]; |
| 35 | + } ); |
| 36 | + |
| 37 | + it( 'creates complete mode files with the original names, types, descriptions, and scopes', () => { |
| 38 | + expect( files.map( ( file ) => file.filename ) ).toEqual( [ |
| 39 | + 'radius/none.json', |
| 40 | + 'radius/subtle.json', |
| 41 | + 'radius/moderate.json', |
| 42 | + 'radius/pronounced.json', |
| 43 | + 'width/standard.json', |
| 44 | + 'width/high-dpi.json', |
| 45 | + ] ); |
| 46 | + |
| 47 | + for ( const file of files ) { |
| 48 | + const group = file.filename.split( '/' )[ 0 ]; |
| 49 | + const document = JSON.parse( file.contents ); |
| 50 | + expect( Object.keys( document ) ).toEqual( [ 'wpds-border' ] ); |
| 51 | + expect( Object.keys( document[ 'wpds-border' ] ) ).toEqual( [ |
| 52 | + group, |
| 53 | + ] ); |
| 54 | + const tokens = document[ 'wpds-border' ][ group ]; |
| 55 | + const names = Object.keys( border[ group ] ).filter( |
| 56 | + ( name ) => ! name.startsWith( '$' ) |
| 57 | + ); |
| 58 | + expect( Object.keys( tokens ) ).toEqual( names ); |
| 59 | + for ( const name of names ) { |
| 60 | + expect( tokens[ name ] ).toEqual( { |
| 61 | + $type: 'dimension', |
| 62 | + $value: { value: expect.any( Number ), unit: 'px' }, |
| 63 | + $description: border[ group ][ name ].$description, |
| 64 | + $extensions: border[ group ][ name ].$extensions, |
| 65 | + } ); |
| 66 | + } |
| 67 | + } |
| 68 | + } ); |
| 69 | +} ); |
0 commit comments