-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#27] feat: add template literal pre-processing tests
- Loading branch information
Showing
9 changed files
with
218 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/* eslint-disable no-console */ | ||
module.exports = async function (_globalConfig, _projectConfig) { | ||
// console.log(globalConfig.testPathPattern); | ||
// console.log(projectConfig.cache); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// ES6 imports | ||
import styled from 'styled'; | ||
import { mod2 } from 'm2'; | ||
|
||
// CJS | ||
const mod3 = require('m3'); | ||
|
||
import { styled } from "@linaria/react"; | ||
|
||
const StyledContainer = styled.div` | ||
:root { | ||
--h1: 44px; | ||
--h2: 32px; | ||
--h3: 24px; | ||
--color-red: red; | ||
--color-green: green; | ||
--color-blue: blue; | ||
} | ||
`; | ||
|
||
const baseClass = styled.css` | ||
--base-var-1: #333; | ||
.child { | ||
--child-var-1: #222; | ||
} | ||
`; | ||
|
||
export const RandomComponent = () => { | ||
const classes = [baseClass, "bar"]; | ||
|
||
//#region Possible Edge cases for Template Literal Parsing | ||
/** | ||
* All the below console.logs causes issue, but for now let's neglect it unless someone raises any issue | ||
* This is because, I don't want to officially support JS/TS parsing, as it's a lot of overhead. | ||
*/ | ||
// If Ever it comes to fix the below template literal, one of the solution could be to: | ||
// To have a counter for colon and semi-colon, and there count should be at-least one at the | ||
// end of parsing a template literal, to make sure the template-literal is possibly a CSS string. | ||
console.log(`--random: error`); | ||
console.log(`-- foo: ${"foo"}`); | ||
console.log(`--fuzz: ${"fuzz"}`); | ||
console.log(`--flex: `, "flex"); | ||
//#endregion | ||
|
||
console.log(`>>> bob--flex: `, "flex"); | ||
|
||
return ( | ||
<StyledContainer className={`${classes[0]}`}> | ||
<p className={`--baz ${classes[1] === "bar" ? "bizz" : "fizz"}`}> | ||
Hell World | ||
</p> | ||
</StyledContainer> | ||
); | ||
}; | ||
|
||
|
||
export { | ||
StyledContainer | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import { resolve } from "path"; | ||
import fs from "fs"; | ||
import preProcessContent, { JS_BLOCK } from "../../pre-processors"; | ||
import { parseFiles } from "../../parser"; | ||
import { CACHE, Config, CSSVarRecord, DEFAULT_CONFIG } from "../../constants"; | ||
|
||
jest.mock("../../constants", () => { | ||
const CONSTANTS = jest.requireActual("../../constants"); | ||
const activeRootPath = "foo"; | ||
return { | ||
__esModule: true, | ||
...CONSTANTS, | ||
CACHE: { | ||
...CONSTANTS.CACHE, | ||
activeRootPath, | ||
config: { [activeRootPath]: CONSTANTS.DEFAULT_CONFIG }, | ||
}, | ||
}; | ||
}); | ||
|
||
type ConfigRecord = { [rootPath: string]: Config }; | ||
|
||
const MODIFIED_DATE = new Date("2021-04-12T08:58:58.676Z"); | ||
const JSX_FILE_PATH = resolve(__dirname, "..", "fixtures", "edge-cases.jsx"); | ||
const MOCK_CONFIG: ConfigRecord = { | ||
[CACHE.activeRootPath]: { | ||
...DEFAULT_CONFIG, | ||
files: [JSX_FILE_PATH], | ||
}, | ||
}; | ||
|
||
const RESULTS = [ | ||
{ | ||
type: "css", | ||
property: "--h1", | ||
value: "44px", | ||
theme: "", | ||
}, | ||
{ | ||
type: "css", | ||
property: "--h2", | ||
value: "32px", | ||
theme: "", | ||
}, | ||
{ | ||
type: "css", | ||
property: "--h3", | ||
value: "24px", | ||
theme: "", | ||
}, | ||
{ | ||
type: "css", | ||
property: "--color-red", | ||
value: "red", | ||
theme: "", | ||
color: "rgb(255, 0, 0)", | ||
}, | ||
{ | ||
type: "css", | ||
property: "--color-green", | ||
value: "green", | ||
theme: "", | ||
color: "rgb(0, 128, 0)", | ||
}, | ||
{ | ||
type: "css", | ||
property: "--color-blue", | ||
value: "blue", | ||
theme: "", | ||
color: "rgb(0, 0, 255)", | ||
}, | ||
{ | ||
type: "css", | ||
property: "--base-var-1", | ||
value: "#333", | ||
theme: "", | ||
color: "rgb(51, 51, 51)", | ||
}, | ||
{ | ||
type: "css", | ||
property: "--child-var-1", | ||
value: "#222", | ||
theme: "", | ||
color: "rgb(34, 34, 34)", | ||
}, | ||
|
||
// This should belong in WRONG_RESULTS, but for now | ||
// it's fine | ||
{ | ||
type: "css", | ||
property: "--random", | ||
value: "error", | ||
theme: "", | ||
}, | ||
]; | ||
|
||
const WRONG_RESULTS = [ | ||
{ | ||
type: "css", | ||
property: "--", | ||
value: JS_BLOCK, | ||
}, | ||
{ | ||
type: "css", | ||
property: "--fuzz", | ||
value: JS_BLOCK, | ||
}, | ||
{ | ||
type: "css", | ||
property: "--flex", | ||
}, | ||
{ | ||
type: "css", | ||
property: "--baz", | ||
}, | ||
]; | ||
|
||
let globalVarRecord: CSSVarRecord; | ||
beforeAll(() => { | ||
CACHE.filesToWatch[CACHE.activeRootPath] = new Set(); | ||
CACHE.fileMetas = {}; | ||
CACHE.fileMetas[JSX_FILE_PATH] = { | ||
path: JSX_FILE_PATH, | ||
lastModified: +MODIFIED_DATE, | ||
}; | ||
return parseFiles(MOCK_CONFIG).then(([varRecord, _]) => { | ||
globalVarRecord = varRecord; | ||
return varRecord; | ||
}); | ||
}); | ||
|
||
test("should have proper variables present", () => { | ||
for (const result of RESULTS) { | ||
expect(globalVarRecord[JSX_FILE_PATH]).toContainEqual(expect.objectContaining(result)); | ||
} | ||
}); | ||
|
||
test("should not have improper variables", () => { | ||
for (const wrong of WRONG_RESULTS) { | ||
expect(globalVarRecord[JSX_FILE_PATH]).not.toContainEqual(expect.objectContaining(wrong)); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters