Skip to content

Commit

Permalink
#10 Purize bem-core tests. #39 Add helper to have blocks despite post…
Browse files Browse the repository at this point in the history
…-processor clearing
  • Loading branch information
Andrii Kirmas committed Mar 12, 2021
1 parent b64a3d4 commit ef757ff
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 127 deletions.
6 changes: 3 additions & 3 deletions __recipes__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import type {ClassHash, ReactClassNaming} from "react-classnaming"
declare module "react-classnaming" {
namespace ReactClassNaming {
interface BemOptions {
// TODO #30 elementDelimiter: "_";
elementDelimiter: "_";
modDelimiter: "-";
}
}
}

setOptions({
// TODO #30 elementDelimiter: "_",
elementDelimiter: "_",
modDelimiter: "-",
})

type CssModule = Record<
"block"|"block-m"
|"block-m"
|"block_el"|"block_el-m-X"|"block_el-m-Y",
ClassHash
>
Expand Down
12 changes: 6 additions & 6 deletions __sandbox__/bem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ describe("upon delimiter", () => {
type Elements<
classes extends string,
b extends string,
delE extends string = "__" /*"elementDelimiter" extends keyof ReactClassNaming.BemOptions
delE extends string = "elementDelimiter" extends keyof ReactClassNaming.BemOptions
? ReactClassNaming.BemOptions["elementDelimiter"]
: ReactClassNaming.BemOptions["$default"]["elementDelimiter"]*/,
: ReactClassNaming.BemOptions["$default"]["elementDelimiter"],
delM extends string = "modDelimiter" extends keyof ReactClassNaming.BemOptions
? ReactClassNaming.BemOptions["modDelimiter"]
: ReactClassNaming.BemOptions["$default"]["modDelimiter"],
Expand All @@ -172,9 +172,9 @@ describe("upon delimiter", () => {

type BemQuery<
classes extends string,
delE extends string = "__" /*"elementDelimiter" extends keyof ReactClassNaming.BemOptions
delE extends string = "elementDelimiter" extends keyof ReactClassNaming.BemOptions
? ReactClassNaming.BemOptions["elementDelimiter"]
: ReactClassNaming.BemOptions["$default"]["elementDelimiter"]*/,
: ReactClassNaming.BemOptions["$default"]["elementDelimiter"],
delM extends string = "modDelimiter" extends keyof ReactClassNaming.BemOptions
? ReactClassNaming.BemOptions["modDelimiter"]
: ReactClassNaming.BemOptions["$default"]["modDelimiter"],
Expand Down Expand Up @@ -211,9 +211,9 @@ describe("upon delimiter", () => {
classes extends string,
b extends string,
e extends string,
delE extends string = "__" /*"elementDelimiter" extends keyof ReactClassNaming.BemOptions
delE extends string = "elementDelimiter" extends keyof ReactClassNaming.BemOptions
? ReactClassNaming.BemOptions["elementDelimiter"]
: ReactClassNaming.BemOptions["$default"]["elementDelimiter"]*/,
: ReactClassNaming.BemOptions["$default"]["elementDelimiter"],
delM extends string = "modDelimiter" extends keyof ReactClassNaming.BemOptions
? ReactClassNaming.BemOptions["modDelimiter"]
: ReactClassNaming.BemOptions["$default"]["modDelimiter"],
Expand Down
4 changes: 2 additions & 2 deletions src/bem.core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe(bem2arr.name, () => {

describe("optioning", () => {
const defaultOpts: BemOptions = {
// TODO #30 elementDelimiter: "__",
elementDelimiter: "__",
modDelimiter: "--",
}

Expand All @@ -56,7 +56,7 @@ describe("optioning", () => {
it("set another", () => {
//TODO #29 test bem with not default options
const opts: BemOptions = {
// TODO #30 elementDelimiter: "_",
elementDelimiter: "_",
modDelimiter: "-",
}
setOptions(opts)
Expand Down
9 changes: 5 additions & 4 deletions src/bem.core.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { BemInGeneral } from "./bem.types"

let modDelimiter = "--"
// TODO #30 , elementDelimiter = "__"
, elementDelimiter = "__"

export type BemOptions = {
// TODO #30 elementDelimiter: string
elementDelimiter: string
modDelimiter: string
}

Expand Down Expand Up @@ -46,15 +46,16 @@ function bem2arr(query: BemInGeneral) {
}

function setOptions({
// TODO #30 elementDelimiter: elD = elementDelimiter,
elementDelimiter: elD = elementDelimiter,
modDelimiter: modDel = modDelimiter
}: Partial<BemOptions>) {
modDelimiter = modDel
elementDelimiter = elD
}

function getOptions() {
return {
// TODO #30 elementDelimiter,
elementDelimiter,
modDelimiter,
}
}
150 changes: 40 additions & 110 deletions src/bem.types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,146 +2,76 @@ import type {BemQuery} from "./bem.types"

describe("BemQuery", () => {
it("block", () => {
const {block} = {} as Record<string, undefined>

const checks: Record<string, BemQuery<"block">> = {
//@ts-expect-error
"number": {block: 1},
"true": {block: true},
"undefined": {block},

/** No modifier - couldn't be false as return of condition */
//@ts-expect-error
"false": {block: false},
//@ts-expect-error
"{}": {block: {}},
}
expect(checks).toBeInstanceOf(Object)
})

it("block__el", () => {
const checks: Record<string, BemQuery<"block__el">> = {
//@ts-expect-error
"number": {block: 1},
//@ts-expect-error
"true": {block: true},
//@ts-expect-error
"{}": {block: {}},
//@ts-expect-error
"el=1": {block: {el: {}}},
//@ts-expect-error
"el": {block: {el: true}},
"block__el": {block__el: true}
}
expect(checks).toBeInstanceOf(Object)
})

it("block__el--mod", () => {
const checks: Record<string, BemQuery<"block__el--mod">> = {
//@ts-expect-error
"number": {block: 1},
//@ts-expect-error
"true": {block: true},
//@ts-expect-error
"{}": {block: {}},
//@ts-expect-error
"el=1": {block: {el: 1}},
//@ts-expect-error`
"el": {block: {el: true}},
//@ts-expect-error
"el: mod": {block: {el: "mod"}},
//@ts-expect-error
"el: [mod]": {block: {el: ["mod"]}},
//@ts-expect-error
"el: -mod": {block: {el: {mod: false}}},
//@ts-expect-error
"el: +mod": {block: {el: {mod: true}}},

//@ts-expect-error
"block__el=1": {block__el: 1},
"block__el": {block__el: true},
"block__el: mod": {block__el: "mod"},
//@ts-expect-error //TODO #40
"block__el: [mod]": {block__el: ["mod"]},
"block__el: -mod": {block__el: {mod: false}},
"block__el: +mod": {block__el: {mod: true}}
}
expect(checks).toBeInstanceOf(Object)
})

it("block__el--mod--val", () => {
const checks: Record<string, BemQuery<"block__el--mod--val">> = {
//@ts-expect-error
"number": {block: 1},
//@ts-expect-error
"true": {block: true},
//@ts-expect-error
"{}": {block: {}},
//@ts-expect-error
"el=1": {block__el: 1},
"el": {block__el: true},
//@ts-expect-error
"el: mod": {block__el: "mod"},
//@ts-expect-error
"el: mod": {block__el: ["mod"]},
"el: -mod": {block__el: {mod: false}},
//@ts-expect-error
"el: +mod": {block__el: {mod: true}},
"el: mod=val": {block__el: {mod: "val"}},
/** `block` may be ommited in CSS post-processor but it exists in ontology */
"block": {block: true},
}
expect(checks).toBeInstanceOf(Object)
})
})

it("block--mod", () => {
const checks: Record<string, BemQuery<"block--mod">> = {
//@ts-expect-error
"number": {block: 1},
"true": {block: true},
/** Numbers are not allowed */
//@ts-expect-error
"0": {block: 0},
//@ts-expect-error
"1": {block: 1},

/** Possible Output of applied conditions for modifications */
"false": {block: false},
"{}": {block: {}},

/** Possible mod apply options */
"mod": {block: "mod"},
"{}": {block: {}},
//@ts-expect-error
"el": {block: {el: true}},
//@ts-expect-error
"$": {block: {$: true}},
//@ts-expect-error
"$: mod": {block: {$: "mod"}},
//@ts-expect-error
"$: +mod": {block: {$: {mod: true}}},
//@ts-expect-error
"$: -mod": {block: {$: {mod: false}}},
"mod-": {block: {mod: false}},
"mod+": {block: {mod: true}},
"-mod": {block: false && {mod: true}},
}
expect(checks).toBeInstanceOf(Object)
})

it("block--mod--val", () => {
const checks: Record<string, BemQuery<"block--mod--val">> = {
//@ts-expect-error
"number": {block: 1},
"true": {block: true},
/** Modifier should have value */
//@ts-expect-error
"mod": {block: "mod"},
"{}": {block: {}},
//@ts-expect-error
"el": {block: {el: true}},
//@ts-expect-error
"mod---val": {block: "mod--val"},
//@ts-expect-error,
"mod+": {block: {"mod": true}},
"mod-": {block: {"mod": false}},
"mod:val": {block: {"mod": "val"}},
//@ts-expect-error
"$": {block: {$: true}},
//@ts-expect-error
"$: mod": {block: {$: "mod"}},
//@ts-expect-error
"$: mod--val": {block: {$: "mod--val"}},
//@ts-expect-error
"$: +mod--val": {block: {$: {"mod--val": true}}},
//@ts-expect-error
"$: -mod--val": {block: {$: {"mod--val": false}}},
//@ts-expect-error
"$: -mod": {block: {$: {mod: false}}},
//@ts-expect-error
"$: +mod": {block: {$: {mod: true}}},
//@ts-expect-error
"$: mod=val": {block: {$: {mod: "val"}}},
"mod+": {block: {mod: true}},

/** Usage */
"mod-": {block: {mod: false}},
"mod: val": {block: {mod: "val"}},
}
expect(checks).toBeInstanceOf(Object)
})

it("#40 array of midfiers", () => {
type Single = BemQuery<"block--modB"|"block--modV--val">
//@ts-expect-error
const modB_string: Single = {block: ["modB"]}
//@ts-expect-error
, modB_obj: Single = {block: [{modB: true}]}
//@ts-expect-error
, modV_obj: Single = {block: [{modV: "val"}]}
})

it("mix #1", () => {
const checks: Record<string, BemQuery<
`${"block" | "block__el"}--${"mod1"|"mod2--val1"|"mod2--val2"}`
Expand Down
5 changes: 4 additions & 1 deletion src/bem.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,14 @@ export type ClassBeming<

export type BemQuery<
classes extends string,
delE extends string = "elementDelimiter" extends keyof ReactClassNaming.BemOptions
? ReactClassNaming.BemOptions["elementDelimiter"]
: ReactClassNaming.BemOptions["$default"]["elementDelimiter"],
delM extends string = "modDelimiter" extends keyof ReactClassNaming.BemOptions
? ReactClassNaming.BemOptions["modDelimiter"]
: ReactClassNaming.BemOptions["$default"]["modDelimiter"],
> = string extends classes ? BemInGeneral : PartDeep<{
[base in Strip<classes, delM>]: true
[base in Strip<classes, delM> | Strip<Strip<classes, delM>, delE>]: true
| (
Extends<classes, `${base}${delM}${string}`,
false
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export { setOptions } from "./bem.core"
export declare namespace ReactClassNaming {
export interface BemOptions {
$default: {
// TODO #30 elementDelimiter: "__"
elementDelimiter: "__",
modDelimiter: "--"
}
}
Expand Down

0 comments on commit ef757ff

Please sign in to comment.